62,616
社区成员
发帖
与我相关
我的任务
分享
public class Example12_9_2 {
public static void main(String[] args) {
ThreadJoin_2 room = new ThreadJoin_2();
Thread customer = new Thread(room,"顾客");
Thread cakeMaker = new Thread(room,"蛋糕师");
room.setJoinThread(cakeMaker);
customer.start();
}
}
public class ThreadJoin_2 implements Runnable{
Thread joinThread;
private Object obj = new Object();
public void setJoinThread(Thread joinThread) {
this.joinThread = joinThread;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"正在等待");
try {
joinThread.start();
joinThread.join();
} catch (InterruptedException |IllegalThreadStateException e) {
System.out.println(Thread.currentThread().getName()+" --- 发生了异常 --- "+e.getLocalizedMessage());
} finally {
System.out.println(Thread.currentThread().getName()+"正在执行");
}
if(Thread.currentThread().getName().equals("顾客")) {
System.out.println("顾客准备吃蛋糕");
}
if(Thread.currentThread().getName().equals("蛋糕师")) {
System.out.println("蛋糕师再做蛋糕");
}
}
}