fixed minor bugs
This commit is contained in:
1 parent
03fd34017e
commit
e4148059a5
2 files changed
+27
-21
No files matched your search
+27
-20
@@ -55,30 +55,40 @@ public class TrafficController {
|
||||
if (!currentVehicle.hasRun) {
|
||||
return 0;
|
||||
} else {
|
||||
System.out.println(currentVehicle.getType() + " " + currentVehicle.id + " crosses the bridge.");
|
||||
int t = currentVehicle.getCrossingTime();
|
||||
queue.remove(currentVehicle);
|
||||
currentVehicle.leave(this);
|
||||
currentVehicle = null;
|
||||
bridge.release();
|
||||
crossings++;
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
for (Vehicle vehicle : queue) {
|
||||
if (vehicle instanceof EmergencyVehicle) {
|
||||
currentVehicle = vehicle;
|
||||
break;
|
||||
// North is ZERO so look for South going car
|
||||
} else if (vehicle.direction == 1 && (northLast || northSemaphore.availablePermits() == 10)) {
|
||||
currentVehicle = vehicle;
|
||||
northLast = false;
|
||||
break;
|
||||
} else if (vehicle.direction == 0 && (!northLast || southSemaphore.availablePermits() == 10)) {
|
||||
currentVehicle = vehicle;
|
||||
northLast = true;
|
||||
break;
|
||||
|
||||
if (emergencySemaphore.availablePermits() != 10) {
|
||||
for (Vehicle v : queue) {
|
||||
if (v instanceof EmergencyVehicle) {
|
||||
currentVehicle = v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (Vehicle vehicle : queue) {
|
||||
// North is ZERO so look for South going car
|
||||
if (vehicle.direction == 1 && (northLast || northSemaphore.availablePermits() == 10)) {
|
||||
currentVehicle = vehicle;
|
||||
northLast = false;
|
||||
break;
|
||||
} else if (vehicle.direction == 0 && (!northLast || southSemaphore.availablePermits() == 10)) {
|
||||
currentVehicle = vehicle;
|
||||
northLast = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (currentVehicle == null) {
|
||||
if (!queue.isEmpty()) {
|
||||
System.out.println("Queue is NOT empty");
|
||||
@@ -89,18 +99,15 @@ public class TrafficController {
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
bridge.acquire();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println(currentVehicle.getType() + " " + currentVehicle.id + " could not cross.");
|
||||
if (!bridge.tryAcquire()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
System.out.println(currentVehicle.getType() + " " + currentVehicle.id + " starts crossing the bridge.");
|
||||
runningVehicle = new Thread(currentVehicle);
|
||||
runningVehicle.start();
|
||||
|
||||
return currentVehicle.getCrossingTime();
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Semaphore getNorthSemaphore() {
|
||||
|
||||
@@ -57,7 +57,6 @@ public abstract class Vehicle implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(this.crossingTime);
|
||||
System.out.println(this.getType() + " " + id + " crosses the bridge.");
|
||||
hasRun = true;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in new issue
Block a user