join字面意思是加入,我理解为插队.
举例:妈妈在炒菜,发现没哟酱油了,让儿子去打酱油,儿子打完酱油,妈妈炒完菜,全家一起吃
package cn.yh.thread01;
/**
*
* 打酱油的例子
*/
public class Demo03 {
public static void main(String[] args) {
Mum mum = new Mum();
mum.start();
}
}
class Mum extends Thread{
@Override
public void run() {
System.out.println("老妈洗菜");
System.out.println("老妈切菜");
System.out.println("老妈开始炒菜");
System.out.println("老妈没有酱油了");
System.out.println("老妈让儿子去打酱油");
Son son = new Son();
son.start();
try {
son.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("老妈炒好了");
System.out.println("全家人一起吃饭");
}
}
class Son extends Thread{
@Override
public void run() {
System.out.println("儿子下楼");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("儿子买完酱油了");
System.out.println("儿子把酱油交给妈妈");
}
}
注意:join方法,在start()方法之后执行.