diff --git a/src/MakkahCity.java b/src/MakkahCity.java index 11831f7..41ff1ae 100644 --- a/src/MakkahCity.java +++ b/src/MakkahCity.java @@ -21,7 +21,7 @@ public class MakkahCity { private static PDate currenttimeManager = firstDayTimeMan; public static void main(String[] args) { - + ArrayList threads = new ArrayList<>(); //Gen Camp campPerDistrict[District.ALMANSOOR.ordinal()] = new ArrayList<>(); campPerDistrict[District.ALAZIZIYA.ordinal()] = new ArrayList<>(); @@ -62,27 +62,21 @@ public class MakkahCity { } clearDoneCivilVehicles(); for (Vehicle vehicle : listOfVehicles) { - Route route = vehicle.getRoute(); - double currentLocation = vehicle.getCurrentLocation(); - if (vehicle.getCurrentStreet() == null && - route.getStreets()[0].capcityPoint(0,1000) < 1) { - vehicle.setCurrentStreet(route.getStreets()[0]); - } - if (vehicle.getCurrentStreet() != null && vehicle.getCurrentStreet().capcityPoint(currentLocation, - currentLocation+1000) < 1 ) { - - if (currentLocation >= vehicle.getCurrentStreet().getLength()) { - //Move to next street - vehicle.moveToNextStreet(); - } - if (!vehicle.isArrivedToDest()) { - double factor = 1-(vehicle.getCurrentStreet().capcityPoint(vehicle.getCurrentLocation(), - vehicle.getCurrentLocation()+1000,vehicle)) ; - if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD * factor ); - else if (vehicle instanceof Sedan) vehicle.move(Sedan.MAX_FORWARD * factor ); - else if (vehicle instanceof SUV) vehicle.move(SUV.MAX_FORWARD * factor ); - else if (vehicle instanceof Truck) vehicle.move(Bus.MAX_FORWARD * factor ); - } + VMover m = new VMover(vehicle); + threads.add(new Thread(m)); + } + for (int i = 0; i < threads.size(); i += 4){ + threads.get(i).start(); + threads.get(i+1).start(); + threads.get(i+2).start(); + threads.get(i+3).start(); + try { + threads.get(i).join(); + threads.get(i+1).join(); + threads.get(i+2).join(); + threads.get(i+3).join(); + } catch (InterruptedException e) { + e.printStackTrace(); } } //noise based on time of day (From PDate) diff --git a/src/VMover.java b/src/VMover.java new file mode 100644 index 0000000..62215ee --- /dev/null +++ b/src/VMover.java @@ -0,0 +1,34 @@ +public class VMover implements Runnable { + + private Vehicle vehicle; + + public VMover(Vehicle vehicle) { + this.vehicle = vehicle; + } + + @Override + public void run() { + Route route = vehicle.getRoute(); + double currentLocation = vehicle.getCurrentLocation(); + if (vehicle.getCurrentStreet() == null && + route.getStreets()[0].capcityPoint(0,1000) < 1) { + vehicle.setCurrentStreet(route.getStreets()[0]); + } + if (vehicle.getCurrentStreet() != null && vehicle.getCurrentStreet().capcityPoint(currentLocation, + currentLocation+1000) < 1 ) { + + if (currentLocation >= vehicle.getCurrentStreet().getLength()) { + //Move to next street + vehicle.moveToNextStreet(); + } + if (!vehicle.isArrivedToDest()) { + double factor = 1-(vehicle.getCurrentStreet().capcityPoint(vehicle.getCurrentLocation(), + vehicle.getCurrentLocation()+1000,vehicle)) ; + if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD * factor ); + else if (vehicle instanceof Sedan) vehicle.move(Sedan.MAX_FORWARD * factor ); + else if (vehicle instanceof SUV) vehicle.move(SUV.MAX_FORWARD * factor ); + else if (vehicle instanceof Truck) vehicle.move(Bus.MAX_FORWARD * factor ); + } + } + } +}