Приветсвую всех, подскажите пожалуйста как быть, разбираюсь с многопоточностью, в данный момент есть одна полоса, и два самолета, взлет происходит тогда когда самолет прогрелся и взлетел один из них, сейчас хочу сделать так, чтобы было две полосы и три самолета, т.е два самолета прогрелись и взлетели, а другой ждал пока осовободится какая-либо из полос?
public class Main {
public static void main(String[] args) throws InterruptedException {
final int min = 1000;
final int max = 10000;
final int rnd = DelayTakeoff.rnd(min,max);
Band newBand = new Band();
Passenger passenger = new Passenger("Пассажирский");
Cargo cargo = new Cargo("Грузовой");
newBand.addPlane(passenger);
newBand.addPlane(cargo);
DelayTakeoff thread1 = new DelayTakeoff(
passenger.name, passenger,newBand,5000,rnd);
DelayTakeoff thread2 = new DelayTakeoff(
cargo.name, cargo,newBand,10000,rnd);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
}
}
public class DelayTakeoff extends Thread {
private final Band object;
private final int delay;
private final int rndDelay;
DelayTakeoff(String name,Plane plane, Band object, int delay, int rndDelay) {
this.setName(name);
this.object = object;
this.delay = delay;
this.rndDelay = rndDelay;
}
public int getDelay() {
return delay;
}
public int getRndDelay() {
return rndDelay;
}
public static int rnd(int min, int max) {
max -=min;
return (int) (Math.random() * ++ max) + min;
}
@Override public void run() {
try {
System.out.println("Прогрев начался: " + getName());
Thread.sleep(getRndDelay());
System.out.println("Прогрев завершился: " + getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (object) {
try {
System.out.println("Взлетает: " + getName());
Thread.sleep(getDelay());
System.out.println("Взлетел: " + getName());
} catch (InterruptedException e1) {}
object.notify();
try {
object.wait();
} catch (InterruptedException e2) {}
}
}
}