Therad.join : 主要會完成一個程式後再跳出.
public class RunnableTest{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CRTest AAA = new CRTest("AAA");
CRTest BBB = new CRTest("BBB");
Thread t1 = new Thread(AAA);
Thread t2 = new Thread(BBB);
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("end main");
}
}
public class CRTest implements Runnable{
private String id;
public CRTest(String s){
id = s;
}
public void run() {
for(int i = 0; i < 4; i++){
int time = (int)(1000*Math.random());
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int j = 0; j < 10000000; j++);
System.out.println(id + " is runing... Time : " + time);
}
}
}
=====================================
AAA is runing... Time : 376
AAA is runing... Time : 46
AAA is runing... Time : 414
AAA is runing... Time : 438
BBB is runing... Time : 158
BBB is runing... Time : 801
BBB is runing... Time : 373
BBB is runing... Time : 864
end main
=====================================
沒有留言:
張貼留言