Задача, вроде, должна решаться элементарно, но валидатор не пропускает её по п.2, пробовал перебирать значения от 80 до 100, задача не проходит, в чем может быть проблема?
package com.javarush.task.task16.task1615;
/*
Аэропорт
*/
public class Solution {
public static volatile Runway RUNWAY = new Runway(); //1 взлетная полоса
public static void main(String[] args) throws InterruptedException {
Plane plane1 = new Plane("Самолет #1");
Plane plane2 = new Plane("Самолет #2");
Plane plane3 = new Plane("Самолет #3");
}
private static void waiting() {
try {
Thread.sleep(100);
Thread.currentThread().interrupt();
} catch (InterruptedException e) {
}
}
private static void takingOff() {
//fix this method - исправь этот метод
try {
Thread.sleep(100);
Thread.currentThread().interrupt();
} catch (InterruptedException e) {
}
}
public static class Plane extends Thread {
public Plane(String name) {
super(name);
start();
}
public void run() {
boolean isAlreadyTakenOff = false;
while (!isAlreadyTakenOff) {
if (RUNWAY.trySetTakingOffPlane(this)) { //если взлетная полоса свободна, занимаем ее
System.out.println(getName() + " взлетает");
takingOff();//взлетает
System.out.println(getName() + " уже в небе");
isAlreadyTakenOff = true;
RUNWAY.setTakingOffPlane(null);
} else if (!this.equals(RUNWAY.getTakingOffPlane())) { //если взлетная полоса занята самолетом
System.out.println(getName() + " ожидает");
waiting(); //ожидает
}
}
}
}
public static class Runway { //взлетная полоса
private Thread t;
public Thread getTakingOffPlane() {
return t;
}
public void setTakingOffPlane(Thread t) {
synchronized (this) {
this.t = t;
}
}
public boolean trySetTakingOffPlane(Thread t) {
synchronized (this) {
if (this.t == null) {
this.t = t;
return true;
}
return false;
}
}
}
}