Callable在多个异步任务里并不能把最先完成的任务返回结果,针对于这个缺点,CompletionService很好的解决这个问题。在CompletionService中提供了提交异步任务的方法(真正的异步任务执行还是由其内部的ExecutorService完成的),任务提交之后调用者不再关注Future,而是从BlockingQueue中获取已经执行完成的Future,在异步任务完成之后Future才会被插入阻塞队列,也就是说调用者从阻塞队列中获取的Future是已经完成了的异步执行任务,所以再次通过Future的get方法获取结果时,调用者所在的当前线程将不会被阻塞

- 验证只要有任务执行完,就会放入队列里-1
public class JucThread03 implements Callable<String> {
private String name;
private Integer sleeps;
public JucThread03(String name, Integer sleep) {
this.name = name;
this.sleeps = sleep;
}
@Override
public String call() throws Exception {
Thread.sleep(sleeps);
return name;
}
}
try {
Callable callable1 = new JucThread03("001", 5000);
Callable callable2 = new JucThread03("002", 10000);
List<Future> futureList = new ArrayList<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 0L, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
CompletionService completionService = new ExecutorCompletionService(threadPoolExecutor);
futureList.add(completionService.submit(callable1));
futureList.add(completionService.submit(callable2));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
System.out.println(sdf.format(new Date()));
System.out.println(completionService.poll(4, TimeUnit.SECONDS) + "时间===" + sdf.format(new Date()));
System.out.println("ddd"+ "时间===" + sdf.format(new Date()));
System.out.println(completionService.poll(8, TimeUnit.SECONDS).get() + "时间===" + sdf.format(new Date()));
System.out.println("ddd"+ "时间===" + sdf.format(new Date()));
System.out.println(completionService.take().get() + "时间===" + sdf.format(new Date()));
System.out.println("主线程完事了" + "时间===" + sdf.format(new Date()));
threadPoolExecutor.shutdown();
} catch (Exception e) {
e.printStackTrace();
}

2. 验证只要有任务执行完,就会放入队列里-2
try {
Callable callable1 = new JucThread03("001", 5000);
Callable callable2 = new JucThread03("002", 10000);
List<Future> futureList = new ArrayList<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 0L, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
CompletionService completionService = new ExecutorCompletionService(threadPoolExecutor);
futureList.add(completionService.submit(callable1));
futureList.add(completionService.submit(callable2));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
System.out.println(sdf.format(new Date()));
for (int i = 0; i < futureList.size(); i++) {
System.out.println(completionService.take().get() + "时间===" + sdf.format(new Date()));
}
System.out.println("主线程完事了" + "时间===" + sdf.format(new Date()));
threadPoolExecutor.shutdown();
} catch (Exception e) {
e.printStackTrace();
}

CompletionService及其实现ExecutorCompletionService,它并不是ExecutorService的一个实现或者子类,而是对ExecutorService提供了进一步的封装,使得任务的提交者不再关注追踪所返回的Future,并且通过CompletionService直接获取已经运算结束的异步任务,这种方式实现了调用者和Future之间的解耦合,在一定程度上解决了Future会使调用者线程进入阻塞的问题,尤其是通过ExecutorService提交批处理任务为如何快速使用最早结束的异步任务运算结果提供了一种新的思路和实现方式。
本文介绍了Java并发库中CompletionService如何解决Callable任务在多个异步执行时不能立即获取最先完成任务结果的问题。通过ExecutorCompletionService,可以实现从已完成的任务队列中获取结果,避免了Future导致的阻塞问题,提高了批处理任务中获取最早结束任务结果的效率。
170

被折叠的 条评论
为什么被折叠?



