vor GPT
This commit is contained in:
@@ -1,5 +1,25 @@
|
|||||||
package UE21_290425_Bruecke;
|
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;
|
package UE21_290425_Bruecke;
|
||||||
|
|
||||||
public class Generator {
|
public class Generator extends Thread {
|
||||||
public static void main(String[] args) {
|
public static boolean RESUME = true;
|
||||||
Guard guard = new Guard();
|
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.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class Guard {
|
public class Guard extends Thread {
|
||||||
private static final List<Car> waitingCars = new ArrayList<>();
|
private List<Car> waitingCars = new ArrayList<>();
|
||||||
private static final Set<Integer> waitingTimes = new HashSet<>();
|
private Set<Integer> waitingTimes = new HashSet<>();
|
||||||
private static int carsOnBridge = 0;
|
private int carsOnBridge = 0;
|
||||||
|
|
||||||
public static void arriving(Car car) {
|
public synchronized void leaving() {
|
||||||
waitingCars.add(car);
|
this.carsOnBridge--;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void leaving() {
|
public synchronized void arriving(Car car) {
|
||||||
carsOnBridge--;
|
System.out.println("add");
|
||||||
|
this.waitingCars.add(car);
|
||||||
|
System.out.println(this.waitingCars.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void crossing(Car car) throws InterruptedException {
|
public synchronized void crossing(Car car) {
|
||||||
carsOnBridge++;
|
System.out.println("crossing");
|
||||||
waitingCars.remove(car);
|
this.carsOnBridge++;
|
||||||
Thread.sleep(50_000);
|
this.waitingCars.remove(car);
|
||||||
leaving();
|
car.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
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