Многопоточность, как можно изменить блок main
как можно привести в более красивый вид, функцию main? Пытался сделать закинуть входным параметром в конструктор 10с для одного и 5с для второго,но он не воспринимает их, делал функцию getDelay и использовал в методе run,сюда представлю рабочий вариант. И еще один вопрос, допустим есть еще один/два потока и они конкурируют между собой, какой из них первый проснулся тот самолет взлетает, как это можно правильно реализовать? Надеюсь более менее понятно объяснил.
public class DelayTakeoff extends Thread {
private Object object;
private int delay;
DelayTakeoff(Band band, Passenger passenger, Object object) {
this.object = object;
}
DelayTakeoff(Band band,Cargo cargo, Object object) {
this.object = object;
}
@Override
public void run() {
synchronized (object) {
object.notify();
try {
object.wait();
} catch (InterruptedException e2) {
}
}
}
}
Далее main
public class Main {
public static void main(String[] args) {
Band newBand = new Band();
Passenger passenger = new Passenger("Пассажирский");
Cargo cargo = new Cargo("Грузовой");
newBand.addPlane(passenger);
newBand.addPlane(cargo);
Object object = new Object();
DelayTakeoff thread1 = new DelayTakeoff(newBand,passenger,object);
DelayTakeoff thread2 = new DelayTakeoff(newBand,cargo,object);
try {
System.out.println(passenger.takeoff());
Thread.sleep(1000);
System.out.println(passenger.getName());
thread1.start();
Thread.sleep(10000);
System.out.println("Взлет!");
Thread.sleep(1000);
System.out.println(cargo.takeoff());
Thread.sleep(1000);
System.out.println(cargo.getName());
thread2.start();
Thread.sleep(5000);
System.out.println("Взлет!");
} catch (InterruptedException e) {}
}
}
Ради теста попытался реализовать, но тайминги не сработали почему то
@Override
public void run() {
synchronized (object) {
System.out.println("123");
try {
Thread.sleep(getDelay());
} catch (InterruptedException e2) {}
object.notify();
try {
object.wait();
} catch (InterruptedException e2) {
}
}
}
}
Object object = new Object();
DelayTakeoff thread1 = new DelayTakeoff(newBand,passenger,object,1000);
DelayTakeoff thread2 = new DelayTakeoff(newBand,cargo,object,3000);
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Ответы (1 шт):
Автор решения: denis Krivorutchko
→ Ссылка
public class Main {
public static void main(String[] args)
throws InterruptedException {
//вы можете запустить все потоки-напритер
// первый параметр в конструкторе ето время взлета
Fly fly = new Fly(10,"passenger1","cargo1");
Fly fly1 = new Fly(15,"passenger2","cargo2");
Fly fly2 = new Fly(20,"passenger3","cargo3");
Thread thread = new Thread(fly);
Thread thread1 = new Thread(fly1);
Thread thread2 = new Thread(fly2);
thread.start();
thread.join();
thread1.start();
thread1.join();
thread2.start();
thread2.join();
System.out.println("The End");
}
}
public class Fly implements Runnable{
public int timeToSleep;
String passenger;
String cargo;
public Fly(int timeToSleep, String passenger, String cargo) {
this.timeToSleep = timeToSleep;
this.passenger = passenger;
this.cargo = cargo;
}
@Override
public void run() {
System.out.println(passenger + " takeOf");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(cargo + " takeOf");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("взлет");
try {
Thread.sleep(timeToSleep * 1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}