package test.Thread;
import java.util.Date;
public class testJoin {
public static void main(String[] args) {
// TODO Auto-generated method stub
Runner4 r4 = new Runner4();
Thread t1 = new Thread(r4);
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
for (int i = 1; i < 5; i++) {
System.out.println("this is main thread");
}
}
}
class Runner4 implements Runnable {
public void run() {
// TODO Auto-generated method stub
for (int i = 1; i < 5; i++) {
System.out.println("this is runner4 " + new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("rnuuer4 is interrupted");
return;
}
}
}
}
the result of not use join
this is main thread
this is main thread
this is main thread
this is main thread
this is runner4 Fri Sep 05 21:40:22 CST 2008
this is runner4 Fri Sep 05 21:40:23 CST 2008
this is runner4 Fri Sep 05 21:40:24 CST 2008
this is runner4 Fri Sep 05 21:40:25 CST 2008
the result of use join
this is runner4 Fri Sep 05 21:46:23 CST 2008
this is runner4 Fri Sep 05 21:46:24 CST 2008
this is runner4 Fri Sep 05 21:46:25 CST 2008
this is runner4 Fri Sep 05 21:46:26 CST 2008
this is main thread
this is main thread
this is main thread
this is main thread