alles Funktioniert

This commit is contained in:
2025-05-13 12:10:01 +02:00
parent 14c5ef09f5
commit ac3690ea14
4 changed files with 45 additions and 42 deletions

View File

@@ -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);

View File

@@ -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);
}
}
}

View File

@@ -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();
}
}
}

View 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;
}