CompletionService的简单用法
解决方法:
1.测试方法,与线程池结合使用
public static void main(String[] args) throws Exception {
CallableDemo callable = new CallableDemo(1,100000);
CallableDemo callable2 = new CallableDemo(1,100);
ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 5, 5L,TimeUnit.SECONDS, new LinkedBlockingDeque());
CompletionService csRef = new ExecutorCompletionService(executor);
csRef.submit(callable);
csRef.submit(callable2);
//CompletionService 的take()方法获取最先执行完的线程的Future对象。
System.out.println(csRef.take().get());
System.out.println(csRef.take().get());
}
2.线程类,实现Callable解决取返回值:http://www.yayihouse.com/yayishuwu/chapter/1945