三种方式实现:多线程执行任务,汇总最终结果;先附上代码,如下
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.*;
public class ExecutorsTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
//创建线程池
ExecutorService exe = Executors.newFixedThreadPool(5);
//生成list
List<Future<Integer>> futureList = new ArrayList<>();
List<FutureTask<Integer>> futureTasksList = new ArrayList<>();
CompletionService<Integer> completionService = new ExecutorCompletionService(exe);
List<Future<Integer>> futureCompletionList = new ArrayList<>();
//初始值
Integer FutureResult = 0;
Integer FutureTaskResult = 0;
Integer completionResult = 0;
//1.ExecutorService+future
for (int i = 1; i <= 10; i++) {
Future future = exe.submit(new CallableTask(i ));
futureList.add(future);
}
while (futureList.size() > 0) {
Iterator<Future<Integer>> iterator = futureList.iterator();
while (iterator.hasNext()) {
Future<Integer> future = iterator.next();
if (future.isDone() && !future.isCancelled()) {
Integer value = future.get();
FutureResult += value;
iterator.remove();
}
}
}
System.out.println("ExecutorService+future最终获取值:" + FutureResult);
//2.ExecutorService+futureTask
for (int i = 1; i <= 10; i++) {
FutureTask<Integer> futureTask = new FutureTask<>(new CallableTask(i));
exe.submit(futureTask);
futureTasksList.add(futureTask);
}
while (futureTasksList.size() > 0) {
Iterator<FutureTask<Integer>> iterator1 = futureTasksList.iterator();
while (iterator1.hasNext()) {
Future<Integer> future = iterator1.next();
if (future.isDone() && !future.isCancelled()) {
Integer value = future.get();
FutureTaskResult += value;
iterator1.remove();
}
}
}
System.out.println("ExecutorService+futureTask最终获取值:" + FutureTaskResult);
//3.CompletionExecutorService+futureTask
for (int i = 1; i <= 10; i++) {
Future<Integer> future = completionService.submit(new CallableTask(i));
futureCompletionList.add(future);
}
while (futureCompletionList.size() > 0) {
Iterator<Future<Integer>> iterator1 = futureCompletionList.iterator();
while (iterator1.hasNext()) {
Future<Integer> future = iterator1.next();
if (future.isDone() && !future.isCancelled()) {
//按照提交顺序返回结果
//Integer value = future.get();
//谁先执行完谁先返回
Integer value= completionService.take().get();
completionResult += value;
iterator1.remove();
}
}
}
System.out.println("CompletionExecutorService+futureTask最终获取值:" + completionResult);
}
}
实现callable接口
import java.util.concurrent.Callable;
public class CallableTask implements Callable {
private Integer value;
public CallableTask(Integer value){
this.value=value;
}
@Override
public Object call() throws Exception {
return value;
}
}
最终执行结果:
第三种方式写了两种提取方式,如图:
第一种 :future.get();因为是按照提交顺序返回结果的,所以会造成阻塞。
第二种:completionService.take().get();是按谁完成谁先返回结果,放到一个队列里再取数据,只会在取数据时没有数据的时候阻塞。如图:
CompletionService<Integer> completionService = new ExecutorCompletionService(exe);
Future<Integer> future = completionService.submit(new CallableTask(i));
completionService.take().get()
记得还有其他的实现方式,也是根据future来返回结果