UE21
This commit is contained in:
@@ -1,22 +1,20 @@
|
|||||||
package UE21_290425_Bruecke;
|
package UE21_290425_Bruecke;
|
||||||
|
|
||||||
import java.time.LocalTime;
|
|
||||||
|
|
||||||
public class Car extends Thread {
|
public class Car extends Thread {
|
||||||
public Long index;
|
public Long index;
|
||||||
public LocalTime arrivingTime;
|
public long arrivingTime;
|
||||||
public Guard guard;
|
public Guard guard;
|
||||||
|
|
||||||
public Car(Guard guard, long index) {
|
public Car(Guard guard, long index) {
|
||||||
this.guard = guard;
|
this.guard = guard;
|
||||||
this.arrivingTime = LocalTime.now();
|
this.arrivingTime = System.currentTimeMillis();
|
||||||
this.index = index;
|
this.index = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(Variables.CROSSING_TIME);
|
Thread.sleep(Variables.CROSSING_TIME * 1000);
|
||||||
guard.leavingBridge();
|
guard.leavingBridge();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ public class Generator extends Thread {
|
|||||||
System.out.println("Statistics:");
|
System.out.println("Statistics:");
|
||||||
generator.start();
|
generator.start();
|
||||||
guard.start();
|
guard.start();
|
||||||
if (Variables.SHOW_CARS) Bridge.main(args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
package UE21_290425_Bruecke;
|
package UE21_290425_Bruecke;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
public class Guard extends Thread {
|
public class Guard extends Thread {
|
||||||
private static int carsOnBridge = 0;
|
private static int carsOnBridge = 0;
|
||||||
public final List<Car> waitingCars = new ArrayList<>();
|
public final List<Car> waitingCars = new ArrayList<>();
|
||||||
public final Set<Integer> waitingTimes = new HashSet<>();
|
public final TreeMap<Long, Long> waitingTimes = new TreeMap<>();
|
||||||
|
|
||||||
public synchronized void leavingBridge() {
|
public synchronized void leavingBridge() {
|
||||||
carsOnBridge--;
|
carsOnBridge--;
|
||||||
@@ -22,18 +21,17 @@ public class Guard extends Thread {
|
|||||||
|
|
||||||
public synchronized void crossingBridge(Car car) {
|
public synchronized void crossingBridge(Car car) {
|
||||||
carsOnBridge++;
|
carsOnBridge++;
|
||||||
|
this.waitingTimes.put(System.currentTimeMillis(), System.currentTimeMillis() - car.arrivingTime);
|
||||||
this.waitingCars.remove(car);
|
this.waitingCars.remove(car);
|
||||||
car.start();
|
car.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized String toString() {
|
public synchronized String toString() {
|
||||||
StringBuilder output = new StringBuilder("Cars waiting:\n");
|
StringBuilder output = new StringBuilder("\nAmount of cars on Bridge: ");
|
||||||
if (waitingCars.isEmpty()) output = new StringBuilder("No Cars waiting:");
|
output.append(carsOnBridge).append("\n").append("Size of queue: ").append(waitingCars.size()).append("\nAverage waiting time of the last ").append(Variables.AVG_PRINT_MINUTES).append(" Minutes: ");
|
||||||
for (Car car : waitingCars)
|
waitingTimes.headMap(System.currentTimeMillis() - Variables.AVG_PRINT_MINUTES * 60_000, false).clear();
|
||||||
output.append(String.format("Car%d:\t%02d:%02d:%02d\t", car.index, car.arrivingTime.getHour(), car.arrivingTime.getMinute(), car.arrivingTime.getSecond()));
|
return output.append((waitingTimes.values().stream().mapToDouble(Long::longValue).average().orElse(0)) / 1000).append(" Seconds").toString();
|
||||||
output.append("\nCars on Bridge: ").append(carsOnBridge).append("\n");
|
|
||||||
return output.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -45,7 +43,7 @@ public class Guard extends Thread {
|
|||||||
if (carsOnBridge < Variables.MAX_CARS_ON_BRIDGE && !this.waitingCars.isEmpty())
|
if (carsOnBridge < Variables.MAX_CARS_ON_BRIDGE && !this.waitingCars.isEmpty())
|
||||||
crossingBridge(this.waitingCars.getFirst());
|
crossingBridge(this.waitingCars.getFirst());
|
||||||
}
|
}
|
||||||
if (System.currentTimeMillis() - lastPrint >= Variables.PRINT_INTERVAL) {
|
if (System.currentTimeMillis() - lastPrint >= Variables.PRINT_INTERVAL * 1000) {
|
||||||
System.out.println(this);
|
System.out.println(this);
|
||||||
lastPrint = System.currentTimeMillis();
|
lastPrint = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package UE21_290425_Bruecke;
|
|||||||
public class Variables {
|
public class Variables {
|
||||||
public static final int MAX_WAITING_CARS = 999_999_999; // Can be used so that the programm doesn't run infinite
|
public static final int MAX_WAITING_CARS = 999_999_999; // Can be used so that the programm doesn't run infinite
|
||||||
public static final int MAX_CAR_INTERVAL = 5; // Maximal Intervall in witch the cars can arrive (1 - X seconds)
|
public static final int MAX_CAR_INTERVAL = 5; // Maximal Intervall in witch the cars can arrive (1 - X seconds)
|
||||||
public static final int CROSSING_TIME = 30_000; // The time a car needs to cross the bridge
|
public static final int CROSSING_TIME = 50; // The time a car needs to cross the bridge (Seconds)
|
||||||
public static final int MAX_CARS_ON_BRIDGE = 3; // Maximal amount of cars on the bridge at the same time
|
public static final int MAX_CARS_ON_BRIDGE = 10; // Maximal amount of cars on the bridge at the same time
|
||||||
public static final int PRINT_INTERVAL = 2_000; // Amount of seconds between every Statistics output
|
public static final int PRINT_INTERVAL = 10; // Amount of seconds between every Statistics output
|
||||||
public static final boolean SHOW_CARS = true; // Decide if you want a graphical visualisation
|
public static final int AVG_PRINT_MINUTES = 10; // Last X minutes AVG waiting time of the cars (Minutes)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user