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
先运行后面的。