请告诉我我在哪里做错了,我的意思是所有项目的顺序输出,根据数字
public class MainClass {
public static void main(String[] args) throws InterruptedException {
ThreadA threadA = new ThreadA();
ThreadB threadB = new ThreadB();
threadA.setThreadB(threadB);
threadB.setThreadA(threadA);
threadA.start();
threadB.start();
}
}
class ThreadA extends Thread{
private Thread threadB;
public void setThreadB(ThreadB threadB) {
this.threadB = threadB;
}
@Override
public void run() {
synchronized (threadB) {
try {
synchronized (this){
System.out.println("1.Получили заказ\n");
wait();
threadB.notify();
System.out.println("3.Обдумали план действий\n");
wait();
threadB.notify();
System.out.println("5.Проконтролировали корректное выполнеие\n");
wait();
threadB.notify();
System.out.println("7.Получили деньги\n");
wait();
threadB.notify();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ThreadB extends Thread{
private Thread threadA;
public void setThreadA(ThreadA threadA) {
this.threadA = threadA;
}
@Override
public void run() {
synchronized (threadA){
synchronized (this){
try {
wait();
System.out.println("2.Состававили ТЗ\n");
threadA.notify();
wait();
System.out.println("4.Наняли работяг\n");
threadA.notify();
wait();
System.out.println("6.Сдали работу\n");
threadA.notify();
wait();
System.out.println("8.Пошли отмечать\n");
threadA.notify();
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
}
仅显示以下内容: 1.收到订单
1 个回答