您的位置:首页 > 编程语言 > Java开发

java join 多线程顺序执行

2015-10-30 00:00 357 查看
public static void main(String[] args) throws InterruptedException {

final List<Thread> threads = new ArrayList<Thread>();
for(int i=0;i<100;i++){
class A extends Thread{
int i ;
public A(int i){
this.i = i;
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread " + i);
}
}
threads.add(new A(i));
}
//倒叙
Collections.reverse(threads);

for(int i=99;i>=0;i--){
threads.get(i).start();
if(i!=0){
threads.get(i).join();
}
}
}

关键点:

线程执行后才能join

先运行后面的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  多线程