Future
Future
用于异步结果计算。它提供了一些方法来检查计算是否完成,使用get
方法将阻塞线程直到结果返回
cancel
:尝试取消任务的执行,如果任务已完成或已取消,此操作无效isCancelled
:任务是否已取消isDone
:任务是否已完成get
:阻塞线程以获取计算结果,直至任务执行完毕返回结果get(long timeout, TimeUnit unit)
:阻塞线程以获取计算结果,若在指定时间没返回结果,则返回null
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
Future
结合线程池的使用
public void futureTest(){
ExecutorService executorService = Executors.newFixedThreadPool(10);
Future<String> nickFuture = executorService.submit(() -> userService.getNick());
Future<String> nameFuture = executorService.submit(() -> userService.getUserName());
// 阻塞开始,等待结果
String nick = nickFuture.get(1000, TimeUnit.MILLISECONDS);
String name = nameFuture.get();
}
CompletionService
CompletionService
整合了Executor
和BlockingQueue
的功能。将Callable
任务提交给它去执行,使用take()
和poll()
获取最新完成的任务执行结果.
ExecutorCompletionService
是该接口的实现类,内部有一个线程池和BlockingQueue
队列。它的实现原理其实挺简单:每个提交给ExecutorCompletionService的任务,都会被封装成一个QueueingFuture
(FutureTask
的子类),它重写了done()
方法(该方法会在任务执行完成之后回调),将执行完成的FutureTask
加入到内部队列,take()
和poll()
将得到最新完成的结果FutrueTask
。
使用方法
注:若线程池为局部变量,使用完后需关闭线程池。或直接使用全局变量命名的线程池
public Map<Long, List<String>> batchDoGetLiveRecord(List<LiveRecordReqDTO> reqList) {
Map<Long, List<String>> recordMap = Maps.newHashMap();
// 用线程池executor新建一个CompletionService
CompletionService<Map<Long, List<String>>> completionService = new ExecutorCompletionService<>(executor);
try {
for (LiveRecordReqDTO req : reqList) {
completionService.submit(() -> doGetLiveRecordAsMap(req));
}
for (int i = 0; i < reqList.size(); i++){
Map<Long, List<String>> map = completionService.take().get(1000, TimeUnit.MILLISECONDS);
recordMap.putAll(map);
}
} catch (Exception e) {
logger.warn("batchDoGetLiveRecord fail, uidList:{}, bizId:{}",reqList, bizId, e);
} finally {
completionService.shutdown();
}
return recordMap;
}
类图自CompletionService和ExecutorCompletionService详解
参考资料: