diff --git a/src/UE21_290425_Bruecke/Car.java b/src/UE21_290425_Bruecke/Car.java index d7d97a9..dd69a01 100644 --- a/src/UE21_290425_Bruecke/Car.java +++ b/src/UE21_290425_Bruecke/Car.java @@ -17,10 +17,10 @@ public class Car extends Thread { public void run() { try { Thread.sleep(Variables.CROSSING_TIME); - guard.leaving(); + guard.leavingBridge(); } catch (InterruptedException e) { throw new RuntimeException(e); } } -} +} \ No newline at end of file diff --git a/src/UE21_290425_Bruecke/Generator.java b/src/UE21_290425_Bruecke/Generator.java index 00046f4..d695bb5 100644 --- a/src/UE21_290425_Bruecke/Generator.java +++ b/src/UE21_290425_Bruecke/Generator.java @@ -9,19 +9,20 @@ public class Generator extends Thread { System.out.println("Statistics:"); generator.start(); guard.start(); + if (Variables.SHOW_CARS) Bridge.main(args); } @Override public void run() { while (guard.waitingCars.size() <= Variables.MAX_WAITING_CARS) { try { + //noinspection BusyWait Thread.sleep((int) (Math.random() * Variables.MAX_CAR_INTERVAL + 1) * 1000L); } catch (InterruptedException e) { throw new RuntimeException(e); } Car car = new Car(guard, carIndex++); - guard.arriving(car); + guard.arrivingOnBridge(car); } - } } diff --git a/src/UE21_290425_Bruecke/Guard.java b/src/UE21_290425_Bruecke/Guard.java index 46112e3..83911f3 100644 --- a/src/UE21_290425_Bruecke/Guard.java +++ b/src/UE21_290425_Bruecke/Guard.java @@ -10,17 +10,17 @@ public class Guard extends Thread { public final List waitingCars = new ArrayList<>(); public final Set waitingTimes = new HashSet<>(); - public synchronized void leaving() { + public synchronized void leavingBridge() { carsOnBridge--; } - public synchronized void arriving(Car car) { + public synchronized void arrivingOnBridge(Car 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++; this.waitingCars.remove(car); car.start(); @@ -43,7 +43,7 @@ public class Guard extends Thread { while (this.waitingCars.size() <= Variables.MAX_WAITING_CARS) { synchronized (this) { 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) { System.out.println(this); @@ -51,4 +51,4 @@ public class Guard extends Thread { } } } -} +} \ No newline at end of file diff --git a/src/UE21_290425_Bruecke/Variables.java b/src/UE21_290425_Bruecke/Variables.java index 5f56ab6..8740fe1 100644 --- a/src/UE21_290425_Bruecke/Variables.java +++ b/src/UE21_290425_Bruecke/Variables.java @@ -1,9 +1,10 @@ package UE21_290425_Bruecke; public class Variables { - public static final int MAX_WAITING_CARS = 2; - public static final int MAX_CAR_INTERVAL = 5; - public static final int CROSSING_TIME = 30_000; - public static final int MAX_CARS_ON_BRIDGE = 3; - public static final int PRINT_INTERVAL = 2_000; + 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; // Maximal Intervall in witch the cars can arrive (1 - X seconds) + 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; // Maximal amount of cars on the bridge at the same time + 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 }