多线程执行返回结果
多线程操作
//代码块为什么是这样
public void test() throws InterruptedException, ExecutionException {
Executor executor = Executors.newFixedThreadPool(3);
CompletionService<String> service = new ExecutorCompletionService<>(executor);
for (int i = 0 ; i < 5 ;i++) {
int seqNo = i;
service.submit(new Callable<String>() {
@Override
public String call() throws Exception {
return "HelloWorld-" + seqNo + "-" + Thread.currentThread().getName();
}
});
}
for (int j = 0 ; j < 5; j++) {
System.out.println(service.take().get());
}
}
————————————————
版权声明:本文为CSDN博主「wind瑞」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/windrui/article/details/101366444