fixed minor bugs

This commit is contained in:
JISAUAY committed 2024-10-10 12:55:21 -05:00
1 parent 03fd34017e
commit e4148059a5
2 files changed
+17 -11

No files matched your search

+17 -10
View File
@@ -55,20 +55,28 @@ 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;
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
} else if (vehicle.direction == 1 && (northLast || northSemaphore.availablePermits() == 10)) {
if (vehicle.direction == 1 && (northLast || northSemaphore.availablePermits() == 10)) {
currentVehicle = vehicle;
northLast = false;
break;
@@ -78,6 +86,8 @@ public class TrafficController {
break;
}
}
}
if (currentVehicle == null) {
if (!queue.isEmpty()) {
@@ -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() {
-1
View File
@@ -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();