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