關於join方法的一個例子
package test;
public class JoinTest {
/**
*
* @author Administrator/2012-3-1/上午09:50:15
*/
public static void main(String[] args) {
int threadNumber = 10;
for (int i = 0; i < threadNumber; i++) {
final int threadID = i;
Thread thread = new Thread() {
public void run() {
try {
Thread.sleep((long) (Math.random() * 10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(String.format("threadID:[%s] finished!!", threadID));
}
};
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("main thread finished!!");
}
}
注意: 通過join方法調用子線程是串行到主線程,不是並發
最後更新:2017-04-02 22:16:29