ich bin hier ein wenig Überfordert
package de.codegym.task.task16.task1615;
/*
Flughafen
*/
public class Solution {
public static volatile Runway RUNWAY = new Runway(); // 1 Start-/Landebahn
public static void main(String[] args) throws InterruptedException {
Plane plane1 = new Plane("Plane #1");
Plane plane2 = new Plane("Plane #2");
Plane plane3 = new Plane("Plane #3");
}
private static void waitForTakeoff() {
// schreib hier deinen Code
}
private static void takeOff() {
// Korrigiere diese Methode
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
}
public static class Plane extends Thread {
public Plane(String name) {
super(name);
start();
}
public void run() {
boolean hasAlreadyTakenOff = false;
while (!hasAlreadyTakenOff) {
if (RUNWAY.trySetCurrentPlane(this)) { // wenn die Start-/Ladenbahn verfügbar ist, nehmen wir sie
System.out.println(getName() + " fliegt jetzt");
takeOff();// fliegt jetzt
System.out.println(getName() + " im Himmel");
hasAlreadyTakenOff = true;
RUNWAY.setCurrentPlane(null);
} else if (!this.equals(RUNWAY.getCurrentPlane())) { // wenn die Start-/Landebahn besetzt ist
System.out.println(getName() + " wartet jetzt");
waitForTakeoff(); // wartet jetzt
}
}
}
}
public static class Runway {
private Thread t;
public Thread getCurrentPlane() {
return t;
}
public void setCurrentPlane(Thread t) {
synchronized (this) {
this.t = t;
}
}
public boolean trySetCurrentPlane(Thread t) {
synchronized (this) {
if (this.t == null) {
this.t = t;
return true;
}
return false;
}
}
}
}