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) {
|
if (!currentVehicle.hasRun) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
|
System.out.println(currentVehicle.getType() + " " + currentVehicle.id + " crosses the bridge.");
|
||||||
|
int t = currentVehicle.getCrossingTime();
|
||||||
queue.remove(currentVehicle);
|
queue.remove(currentVehicle);
|
||||||
currentVehicle.leave(this);
|
currentVehicle.leave(this);
|
||||||
currentVehicle = null;
|
currentVehicle = null;
|
||||||
bridge.release();
|
bridge.release();
|
||||||
crossings++;
|
crossings++;
|
||||||
|
return t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Vehicle vehicle : queue) {
|
if (emergencySemaphore.availablePermits() != 10) {
|
||||||
if (vehicle instanceof EmergencyVehicle) {
|
for (Vehicle v : queue) {
|
||||||
currentVehicle = vehicle;
|
if (v instanceof EmergencyVehicle) {
|
||||||
break;
|
currentVehicle = v;
|
||||||
// North is ZERO so look for South going car
|
break;
|
||||||
} else if (vehicle.direction == 1 && (northLast || northSemaphore.availablePermits() == 10)) {
|
}
|
||||||
currentVehicle = vehicle;
|
}
|
||||||
northLast = false;
|
} else {
|
||||||
break;
|
for (Vehicle vehicle : queue) {
|
||||||
} else if (vehicle.direction == 0 && (!northLast || southSemaphore.availablePermits() == 10)) {
|
// North is ZERO so look for South going car
|
||||||
currentVehicle = vehicle;
|
if (vehicle.direction == 1 && (northLast || northSemaphore.availablePermits() == 10)) {
|
||||||
northLast = true;
|
currentVehicle = vehicle;
|
||||||
break;
|
northLast = false;
|
||||||
|
break;
|
||||||
|
} else if (vehicle.direction == 0 && (!northLast || southSemaphore.availablePermits() == 10)) {
|
||||||
|
currentVehicle = vehicle;
|
||||||
|
northLast = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (currentVehicle == null) {
|
if (currentVehicle == null) {
|
||||||
if (!queue.isEmpty()) {
|
if (!queue.isEmpty()) {
|
||||||
System.out.println("Queue is NOT empty");
|
System.out.println("Queue is NOT empty");
|
||||||
@@ -89,18 +99,15 @@ public class TrafficController {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (!bridge.tryAcquire()) {
|
||||||
bridge.acquire();
|
return 0;
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
System.out.println(currentVehicle.getType() + " " + currentVehicle.id + " could not cross.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println(currentVehicle.getType() + " " + currentVehicle.id + " starts crossing the bridge.");
|
System.out.println(currentVehicle.getType() + " " + currentVehicle.id + " starts crossing the bridge.");
|
||||||
runningVehicle = new Thread(currentVehicle);
|
runningVehicle = new Thread(currentVehicle);
|
||||||
runningVehicle.start();
|
runningVehicle.start();
|
||||||
|
|
||||||
return currentVehicle.getCrossingTime();
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Semaphore getNorthSemaphore() {
|
public Semaphore getNorthSemaphore() {
|
||||||
|
|||||||
@@ -57,7 +57,6 @@ public abstract class Vehicle implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(this.crossingTime);
|
Thread.sleep(this.crossingTime);
|
||||||
System.out.println(this.getType() + " " + id + " crosses the bridge.");
|
|
||||||
hasRun = true;
|
hasRun = true;
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
Reference in new issue
Block a user