vor fx
This commit is contained in:
@@ -17,10 +17,10 @@ public class Car extends Thread {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(Variables.CROSSING_TIME);
|
Thread.sleep(Variables.CROSSING_TIME);
|
||||||
guard.leaving();
|
guard.leavingBridge();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,19 +9,20 @@ public class Generator extends Thread {
|
|||||||
System.out.println("Statistics:");
|
System.out.println("Statistics:");
|
||||||
generator.start();
|
generator.start();
|
||||||
guard.start();
|
guard.start();
|
||||||
|
if (Variables.SHOW_CARS) Bridge.main(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (guard.waitingCars.size() <= Variables.MAX_WAITING_CARS) {
|
while (guard.waitingCars.size() <= Variables.MAX_WAITING_CARS) {
|
||||||
try {
|
try {
|
||||||
|
//noinspection BusyWait
|
||||||
Thread.sleep((int) (Math.random() * Variables.MAX_CAR_INTERVAL + 1) * 1000L);
|
Thread.sleep((int) (Math.random() * Variables.MAX_CAR_INTERVAL + 1) * 1000L);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
Car car = new Car(guard, carIndex++);
|
Car car = new Car(guard, carIndex++);
|
||||||
guard.arriving(car);
|
guard.arrivingOnBridge(car);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,17 +10,17 @@ public class Guard extends Thread {
|
|||||||
public final List<Car> waitingCars = new ArrayList<>();
|
public final List<Car> waitingCars = new ArrayList<>();
|
||||||
public final Set<Integer> waitingTimes = new HashSet<>();
|
public final Set<Integer> waitingTimes = new HashSet<>();
|
||||||
|
|
||||||
public synchronized void leaving() {
|
public synchronized void leavingBridge() {
|
||||||
carsOnBridge--;
|
carsOnBridge--;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void arriving(Car car) {
|
public synchronized void arrivingOnBridge(Car car) {
|
||||||
this.waitingCars.add(car);
|
this.waitingCars.add(car);
|
||||||
notify(); // Weckt den Guard auf
|
notify(); // Wacke up the Guard thread
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public synchronized void crossing(Car car) {
|
public synchronized void crossingBridge(Car car) {
|
||||||
carsOnBridge++;
|
carsOnBridge++;
|
||||||
this.waitingCars.remove(car);
|
this.waitingCars.remove(car);
|
||||||
car.start();
|
car.start();
|
||||||
@@ -43,7 +43,7 @@ public class Guard extends Thread {
|
|||||||
while (this.waitingCars.size() <= Variables.MAX_WAITING_CARS) {
|
while (this.waitingCars.size() <= Variables.MAX_WAITING_CARS) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (carsOnBridge < Variables.MAX_CARS_ON_BRIDGE && !this.waitingCars.isEmpty())
|
if (carsOnBridge < Variables.MAX_CARS_ON_BRIDGE && !this.waitingCars.isEmpty())
|
||||||
crossing(this.waitingCars.getFirst());
|
crossingBridge(this.waitingCars.getFirst());
|
||||||
}
|
}
|
||||||
if (System.currentTimeMillis() - lastPrint >= Variables.PRINT_INTERVAL) {
|
if (System.currentTimeMillis() - lastPrint >= Variables.PRINT_INTERVAL) {
|
||||||
System.out.println(this);
|
System.out.println(this);
|
||||||
@@ -51,4 +51,4 @@ public class Guard extends Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
package UE21_290425_Bruecke;
|
package UE21_290425_Bruecke;
|
||||||
|
|
||||||
public class Variables {
|
public class Variables {
|
||||||
public static final int MAX_WAITING_CARS = 2;
|
public static final int MAX_WAITING_CARS = 999_999_999; // Can be used so that the programm doesn't run infinite
|
||||||
public static final int MAX_CAR_INTERVAL = 5;
|
public static final int MAX_CAR_INTERVAL = 5; // Maximal Intervall in witch the cars can arrive (1 - X seconds)
|
||||||
public static final int CROSSING_TIME = 30_000;
|
public static final int CROSSING_TIME = 30_000; // The time a car needs to cross the bridge
|
||||||
public static final int MAX_CARS_ON_BRIDGE = 3;
|
public static final int MAX_CARS_ON_BRIDGE = 3; // Maximal amount of cars on the bridge at the same time
|
||||||
public static final int PRINT_INTERVAL = 2_000;
|
public static final int PRINT_INTERVAL = 2_000; // Amount of seconds between every Statistics output
|
||||||
|
public static final boolean SHOW_CARS = true; // Decide if you want a graphical visualisation
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user