join的代碼
/*
?* join:合并線程,但是理解成插隊線程更符合
?*/
public class BlockedJoin02 {
?? ?public static void main(String[] args) throws InterruptedException {
?? ??? ?System.out.println("媽媽給女兒買麻辣燙的故事");? //媽媽只有給女兒買到女兒才可以吃
?? ??? ?new Thread(new Girl()).start();
?? ??? ?
?? ?}
}
class Girl extends Thread{
?? ?@Override
?? ?public void run() {
?? ??? ?System.out.println("我想吃麻辣燙了,媽媽你給我去買吧");
?? ??? ?Thread t=new Thread(new Mother());? //join的調(diào)用必須實例化對象才可以
?? ??? ?t.start();
?? ??? ?try {
?? ??? ??? ?t.join();
?? ??? ??? ?System.out.println("我狼吐虎咽的吃");
?? ??? ?} catch (InterruptedException e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?
?? ?}
}
class Mother extends Thread{
?? ?@Override
?? ?public void run() {
?? ??? ?System.out.println("媽媽去給你買");
?? ??? ?try {
?? ??? ??? ?
?? ??? ??? ?Thread.sleep(1000*6);
?? ??? ??? ?System.out.println("6秒過去了");
?? ??? ??? ?
?? ??? ?} catch (InterruptedException e) {
?? ??? ??? ?
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ??? ?System.out.println("媽媽給你買到了");
?? ?}
}
?? ?
標(biāo)簽: