第一种方式join
/**
* Date:2016年9月7日下午7:43:13
* Copyright (c) 2016, All Rights Reserved.
*
*/
package ;
import ;
/**
* Description: TODO <br/>
* Date: 2016年9月7日 下午7:43:13 <br/>
* @author xuejianxin
*/
public class JoinTest {
public static void main(String[] args) throws Exception {
("main is begin");
Thread task1=new Thread(new Task(1));
();
Thread task2=new Thread(new Task(2));
();
//等待task1 over
();//task1 join 到当前线程
();
("main is end") ;
}
}
第二种方式 wait
/**
* Date:2016年9月7日下午7:50:04
* Copyright (c) 2016, All Rights Reserved.
*
*/
package ;
import ;
/**
* Description: TODO <br/>
* Date: 2016年9月7日 下午7:50:04 <br/>
* @author xuejianxin
*/
public class WaitTest1 {
public static void main(String[] args) throws Exception {
/**
* 多线程交互 必须有一个标识,这个标识在java 里就是对象本身
*/
("main is begin");
Thread task1=new Thread(new Task(1));
();
Thread task2=new Thread(new Task(2));
();
/*synchronized () {//这样是不行的
();
();
}
*/
synchronized (task1) {
();
//();//这样是不行的
}
synchronized (task2) {
//();//这样是不行的
();
}
("main is end") ;
}
}