/*
* join方法
* 作用:join方法的作用是加入其它线程,执行了join方法的线程会将当前CPU执行权让给加入的线程,直到加入线程执行完毕才会继续执行其它线程;
*/
class Mum extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println("妈妈开始做饭...");
System.out.println("妈妈开始炒菜");
System.out.println("妈妈发现没有了酱油..");
System.out.println("妈妈叫儿子去买酱油");
Son s = new Son();
s.start();
try {
s.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("妈妈继续做饭");
System.out.println("饭做好了,大家一起吃饭");
}
}
class Son extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
System.out.println("小明开始下楼");
System.out.println("小明朝着小卖部走去...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("小明开始打酱油");
System.out.println("小明往回走...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("小明将酱油交给了妈妈");
}
}
public class Demo11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mum mum = new Mum();
mum.start();
}
}