[java]代码库package com.demo.thread;
import java.io.IOException;
public class JoinDemo {
//public static class MyRunnable implements Runnable{
//private String threadName;
//private static int count = 0;
//
//
//public MyRunnable(String threadName) {
//this.threadName = threadName;
//System.out.println("第"+ ++count +"次调用MyRunnable的构造函数 创建了"+threadName);
//}
//
//@Override
//public void run() {
TODO Auto-generated method stub
//
//}
//
//}
private static Thread t2 = null;
private static Thread t1 = null;
public static void main(String[] args) throws IOException{
//MyRunnable mr1 = new MyRunnable("FirstThread");
//MyRunnable mr2 = new MyRunnable("SecondThread");
t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("调用进程t1的run()方法");
int i = 0;
while(true){
try {
Thread.sleep(500);
System.out.println("Thread1:"+ ++i);
if(i == 2)
t2.join();
if(i == 6)
t2.join();
if(i == 20)
{
System.out.println("Thread1运行完毕");
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t1.start();
t2 = new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
System.out.println("进入t2的run()方法");
while(true){
try {
Thread.sleep(500);
System.out.println("Thread2:" + ++i);
if(i == 10){
System.out.println("Thread2运行完毕");
break;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t2.start();
}
}
[代码运行效果截图]
[源代码打包下载]
JoinDemo.rar(50积分)[0 次下载]