You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
878 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

内部通过阻塞队列+FutureTask实现了**任务先完成可优先获取到,即结果按照完成先后顺序排序。**
````java
// 创建线程池
ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE);
CompletionService<Integer> cService = new ExecutorCompletionService<>(pool);
// 向里面扔任务
for (int i = 0; i < TOTAL_TASK; i++) {
//线CompletionService
cService.submit(new WorkTask("ExecTask" + i));
}
// 线
for (int i = 0; i < TOTAL_TASK; i++) {
//CompletionServiceCompletionServiceBlockingQueue
int sleptTime = cService.take().get();
System.out.println(" slept "+sleptTime+" ms ...");
count.addAndGet(sleptTime);
}
````