Compare commits
1 Commits
master
...
threading-
Author | SHA1 | Date | |
---|---|---|---|
625e4c1faf |
@ -26,6 +26,7 @@ public class MakkahCity {
|
||||
|
||||
private static final InputListener inputListener = new InputListener();
|
||||
private static final Thread t = new Thread(inputListener,"InputThread-Makkah");
|
||||
private static final Thread[] threadPool = new Thread[4];
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@ -58,30 +59,28 @@ public class MakkahCity {
|
||||
|
||||
clearDoneCivilVehicles();
|
||||
addCivilVehicleNoise();
|
||||
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 );
|
||||
|
||||
ArrayList<Vehicle> l = (ArrayList<Vehicle>) listOfVehicles.clone();
|
||||
while (l.size() > 0){
|
||||
for (Thread t : threadPool){
|
||||
if (t == null) {
|
||||
t = new Thread();
|
||||
if (!t.isAlive()) {
|
||||
t = new Thread(new StreetUpdater(l.get(0)));
|
||||
t.start();
|
||||
l.remove(l.get(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Thread t : threadPool) {
|
||||
try {
|
||||
t.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (isAllArrived()) allArrivedToArafatTime = (Date)currenttimeManager.getCurrentTime().clone();
|
||||
firstDayTimeMan.step(Calendar.MINUTE, 1);
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ public class Street implements Travelable {
|
||||
|
||||
private double length;
|
||||
private int numberOfLanes;
|
||||
private ArrayList<Vehicle> vehicles; //Current
|
||||
private HashMap<Vehicle, ArrayList<Date>> vehiclesHistory;//History of vehicles
|
||||
private volatile ArrayList<Vehicle> vehicles; //Current
|
||||
private volatile HashMap<Vehicle, ArrayList<Date>> vehiclesHistory;//History of vehicles
|
||||
private StreetNames name;
|
||||
|
||||
|
||||
@ -63,7 +63,7 @@ public class Street implements Travelable {
|
||||
return name;
|
||||
}
|
||||
|
||||
public double capcity() {
|
||||
public synchronized double capcity() {
|
||||
double totalLength = length * numberOfLanes;
|
||||
double totalLenthofCar=0;
|
||||
for(int i=0;i<vehicles.size();i++) {
|
||||
@ -106,7 +106,7 @@ public class Street implements Travelable {
|
||||
vehiclesHistory.get(vehicle).add(MakkahCity.getTimeMan().getCurrentTime());//Add time
|
||||
}
|
||||
|
||||
public double capcityPoint(double min, double max) {
|
||||
public synchronized double capcityPoint(double min, double max) {
|
||||
double totalLength = (max - min) * numberOfLanes;
|
||||
double totalLenthofCar=0;
|
||||
for(int i=0;i<vehicles.size();i++) {
|
||||
|
@ -1,3 +1,4 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
@ -5,14 +6,14 @@ public abstract class Vehicle {
|
||||
|
||||
private double vehicleSize;
|
||||
private Route route;
|
||||
private Street currentStreet;
|
||||
private double currentLocation;
|
||||
private volatile Street currentStreet;
|
||||
private volatile double currentLocation;
|
||||
private double totalDistanceTraveled;
|
||||
private boolean arrivedToDest;
|
||||
private boolean moving;
|
||||
private Date timeStartedMoving;
|
||||
private Date timeOfArrival;
|
||||
private Date timeStartedOnCurrentStreet;
|
||||
private volatile boolean arrivedToDest;
|
||||
private volatile boolean moving;
|
||||
private volatile Date timeStartedMoving;
|
||||
private volatile Date timeOfArrival;
|
||||
private volatile Date timeStartedOnCurrentStreet;
|
||||
|
||||
//Map Street to Minutes
|
||||
private HashMap<Street, Integer> routeTimeHistory = new HashMap<>();
|
||||
@ -180,4 +181,38 @@ public abstract class Vehicle {
|
||||
}
|
||||
}
|
||||
|
||||
class StreetUpdater implements Runnable {
|
||||
|
||||
Vehicle vehicle;
|
||||
|
||||
public StreetUpdater(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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user