java.util.concurrent包下的类详细解释(三)

2014-11-24 08:22:32 · 作者: · 浏览: 6
cute {
public static void main(String[] args) {
BlockingQueue queue = new LinkedBlockingQueue();
ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 1, TimeUnit.DAYS, queue);
for (int i = 0; i < 20; i++) {
final int index = i;
executor.execute(new Runnable() {
public void run() {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(String.format("thread %d finished", index));
}
});
}
executor.shutdown();
}
}
output:
thread 1 finished
thread 0 finished
thread 2 finished
thread 5 finished
thread 3 finished
thread 4 finished
thread 8 finished
thread 7 finished
thread 6 finished
thread 9 finished
thread 11 finished
thread 10 finished
thread 13 finished
thread 14 finished
thread 12 finished
thread 17 finished
thread 15 finished
thread 16 finished
thread 18 finished
thread 19 finished