package test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
private static CountDownLatch mLatch = new CountDownLatch(4);
static int i = 0;
public static void main(String[] args) throws InterruptedException {
//创建线程池,线程数3个
final ExecutorService exec = Executors.newFixedThreadPool(3);
for (int index = 0; index < 4; index++) {
final int NO = index + 1;
Runnable run = new Runnable() {
public void run() {
try {
i = NO;
System.out.println(NO + " working");
Thread.sleep(2000);
} catch (InterruptedException e) {
} finally {
mLatch.countDown();
}
}
};
exec.submit(run);
}
mLatch.await();
System.out.println("finish");
System.out.println(i);
exec.shutdown();
}
}
?
原文:http://chendaiming.iteye.com/blog/2157368