fixed minor bugs
This commit is contained in:
1 parent
03fd34017e
commit
e4148059a5
2 files changed
+17
-11
No files matched your search
+17
-10
@@ -55,20 +55,28 @@ 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) {
|
||||||
|
currentVehicle = v;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (Vehicle vehicle : queue) {
|
||||||
// North is ZERO so look for South going car
|
// 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;
|
currentVehicle = vehicle;
|
||||||
northLast = false;
|
northLast = false;
|
||||||
break;
|
break;
|
||||||
@@ -78,6 +86,8 @@ public class TrafficController {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (currentVehicle == null) {
|
if (currentVehicle == null) {
|
||||||
if (!queue.isEmpty()) {
|
if (!queue.isEmpty()) {
|
||||||
@@ -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