alles Funktioniert
This commit is contained in:
@@ -3,19 +3,20 @@ package UE21_290425_Bruecke;
|
||||
import java.time.LocalTime;
|
||||
|
||||
public class Car extends Thread {
|
||||
private static final int crossingTime = 50_000;
|
||||
LocalTime arrivingTime;
|
||||
Guard guard;
|
||||
public Long index;
|
||||
public LocalTime arrivingTime;
|
||||
public Guard guard;
|
||||
|
||||
public Car(Guard guard) {
|
||||
public Car(Guard guard, long index) {
|
||||
this.guard = guard;
|
||||
this.arrivingTime = LocalTime.now();
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(crossingTime);
|
||||
Thread.sleep(Variables.CROSSING_TIME);
|
||||
guard.leaving();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
@@ -1,26 +1,27 @@
|
||||
package UE21_290425_Bruecke;
|
||||
|
||||
public class Generator extends Thread {
|
||||
public static boolean RESUME = true;
|
||||
private static Guard guard = new Guard();
|
||||
private static Generator generator = new Generator();
|
||||
private static final Guard guard = new Guard();
|
||||
private static final Generator generator = new Generator();
|
||||
private static long carIndex = 0;
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Statistics:");
|
||||
generator.start();
|
||||
guard.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (Generator.RESUME) {
|
||||
while (guard.waitingCars.size() <= Variables.MAX_WAITING_CARS) {
|
||||
try {
|
||||
Thread.sleep((int) (Math.random() * 5 + 1) * 1000L);
|
||||
Thread.sleep((int) (Math.random() * Variables.MAX_CAR_INTERVAL + 1) * 1000L);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Car car = new Car(guard);
|
||||
Car car = new Car(guard, carIndex++);
|
||||
guard.arriving(car);
|
||||
System.out.println(guard);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,56 +6,48 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class Guard extends Thread {
|
||||
private List<Car> waitingCars = new ArrayList<>();
|
||||
private Set<Integer> waitingTimes = new HashSet<>();
|
||||
private int carsOnBridge = 0;
|
||||
private static int carsOnBridge = 0;
|
||||
public final List<Car> waitingCars = new ArrayList<>();
|
||||
public final Set<Integer> waitingTimes = new HashSet<>();
|
||||
|
||||
public synchronized void leaving() {
|
||||
this.carsOnBridge--;
|
||||
carsOnBridge--;
|
||||
}
|
||||
|
||||
public synchronized void arriving(Car car) {
|
||||
System.out.println("add");
|
||||
this.waitingCars.add(car);
|
||||
System.out.println(this.waitingCars.size());
|
||||
notify(); // Weckt den Guard auf
|
||||
}
|
||||
|
||||
|
||||
public synchronized void crossing(Car car) {
|
||||
System.out.println("crossing");
|
||||
this.carsOnBridge++;
|
||||
carsOnBridge++;
|
||||
this.waitingCars.remove(car);
|
||||
car.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder output = new StringBuilder("""
|
||||
Statistics:
|
||||
waiting:\s
|
||||
""");
|
||||
for (int i = 0; i < waitingCars.size(); i++) {
|
||||
output.append("Car").append(i).append(": \t");
|
||||
output.append(waitingCars.get(i).arrivingTime.getHour());
|
||||
output.append(":").append(waitingCars.get(i).arrivingTime.getMinute());
|
||||
output.append(":").append(waitingCars.get(i).arrivingTime.getSecond()).append("\n");
|
||||
}
|
||||
output
|
||||
// .append("waitingTimes: ").append(waitingTimes)
|
||||
.append("carsOnBridge: ").append(carsOnBridge);
|
||||
public synchronized String toString() {
|
||||
StringBuilder output = new StringBuilder("Cars waiting:\n");
|
||||
if (waitingCars.isEmpty()) output = new StringBuilder("No Cars waiting:");
|
||||
for (Car car : waitingCars)
|
||||
output.append(String.format("Car%d:\t%02d:%02d:%02d\t", car.index, car.arrivingTime.getHour(), car.arrivingTime.getMinute(), car.arrivingTime.getSecond()));
|
||||
output.append("\nCars on Bridge: ").append(carsOnBridge).append("\n");
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (Generator.RESUME) {
|
||||
// if (waitingCars.isEmpty()) continue;
|
||||
if (this.carsOnBridge < 10) {
|
||||
// System.out.println("<10");
|
||||
// System.out.println(waitingCars.size());
|
||||
if (!this.waitingCars.isEmpty()) {
|
||||
System.out.println("crossing");
|
||||
long lastPrint = System.currentTimeMillis();
|
||||
while (this.waitingCars.size() <= Variables.MAX_WAITING_CARS) {
|
||||
synchronized (this) {
|
||||
if (carsOnBridge < Variables.MAX_CARS_ON_BRIDGE && !this.waitingCars.isEmpty())
|
||||
crossing(this.waitingCars.getFirst());
|
||||
}
|
||||
if (System.currentTimeMillis() - lastPrint >= Variables.PRINT_INTERVAL) {
|
||||
System.out.println(this);
|
||||
lastPrint = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
src/UE21_290425_Bruecke/Variables.java
Normal file
9
src/UE21_290425_Bruecke/Variables.java
Normal file
@@ -0,0 +1,9 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user