vor GPT
This commit is contained in:
@@ -1,5 +1,25 @@
|
||||
package UE21_290425_Bruecke;
|
||||
|
||||
public class Car {
|
||||
import java.time.LocalTime;
|
||||
|
||||
public class Car extends Thread {
|
||||
private static final int crossingTime = 50_000;
|
||||
LocalTime arrivingTime;
|
||||
Guard guard;
|
||||
|
||||
public Car(Guard guard) {
|
||||
this.guard = guard;
|
||||
this.arrivingTime = LocalTime.now();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(crossingTime);
|
||||
guard.leaving();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,26 @@
|
||||
package UE21_290425_Bruecke;
|
||||
|
||||
public class Generator {
|
||||
public static void main(String[] args) {
|
||||
Guard guard = new Guard();
|
||||
public class Generator extends Thread {
|
||||
public static boolean RESUME = true;
|
||||
private static Guard guard = new Guard();
|
||||
private static Generator generator = new Generator();
|
||||
|
||||
public static void main(String[] args) {
|
||||
generator.start();
|
||||
guard.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (Generator.RESUME) {
|
||||
try {
|
||||
Thread.sleep((int) (Math.random() * 5 + 1) * 1000L);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
Car car = new Car(guard);
|
||||
guard.arriving(car);
|
||||
System.out.println(guard);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,28 +5,58 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class Guard {
|
||||
private static final List<Car> waitingCars = new ArrayList<>();
|
||||
private static final Set<Integer> waitingTimes = new HashSet<>();
|
||||
private static int carsOnBridge = 0;
|
||||
public class Guard extends Thread {
|
||||
private List<Car> waitingCars = new ArrayList<>();
|
||||
private Set<Integer> waitingTimes = new HashSet<>();
|
||||
private int carsOnBridge = 0;
|
||||
|
||||
public static void arriving(Car car) {
|
||||
waitingCars.add(car);
|
||||
public synchronized void leaving() {
|
||||
this.carsOnBridge--;
|
||||
}
|
||||
|
||||
public static void leaving() {
|
||||
carsOnBridge--;
|
||||
public synchronized void arriving(Car car) {
|
||||
System.out.println("add");
|
||||
this.waitingCars.add(car);
|
||||
System.out.println(this.waitingCars.size());
|
||||
}
|
||||
|
||||
public void crossing(Car car) throws InterruptedException {
|
||||
carsOnBridge++;
|
||||
waitingCars.remove(car);
|
||||
Thread.sleep(50_000);
|
||||
leaving();
|
||||
public synchronized void crossing(Car car) {
|
||||
System.out.println("crossing");
|
||||
this.carsOnBridge++;
|
||||
this.waitingCars.remove(car);
|
||||
car.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Statistics:\n" + "waiting: " + waitingCars + "waitingTimes: " + waitingTimes + "carsOnBridge: " + carsOnBridge;
|
||||
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);
|
||||
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");
|
||||
crossing(this.waitingCars.getFirst());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user