从构造方法的申明中可以发现,类ExecutorComletionService需要依赖于Executor对象,大部分的实现也就是使用线程池ThreadPoolExecutor对象。
package cn.java.Concurrency.completionService;
import java.util.concurrent.Callable;
/**
* @author 小石潭记
* @date 2021/12/19 19:06
* @Description: ${todo}
*/
public class MyCallable implements Callable<String> {
private String username;
private long sleepValue;
public MyCallable(String username, long sleepValue) {
this.username = username;
this.sleepValue = sleepValue;
}
@Override
public String call() throws Exception {
System.out.println(username);
Thread.sleep(sleepValue);
return "return " + username;
}
}
package cn.java.Concurrency.completionService;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
/**
* @author 小石潭记
* @date 2021/12/19 19:08
* @Description: ${todo}
*/
public class Test {
public static void main(String[] args) throws Exception {
MyCallable callable1 = new MyCallable("username1", 5000);
MyCallable callable2 = new MyCallable("username2", 4000);
MyCallable callable3 = new MyCallable("username3", 3000);
MyCallable callable4 = new MyCallable("username4", 2000);
MyCallable callable5 = new MyCallable("username5", 1000);
List<Callable> callableList = new ArrayList<>();
callableList.add(callable1);
callableList.add(callable2);
callableList.add(callable3);
callableList.add(callable4);
callableList.add(callable5);
ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
10, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
ExecutorCompletionService completionService = new ExecutorCompletionService<>(executor);
for (int i = 0; i < 5; i++) {
completionService.submit(callableList.get(i));
}
for (int i = 0; i < 5; i++) {
System.out.println("等待打印第" + (i + 1) + "个返回值");
System.out.println(completionService.take().get());
}
// 按乱序打印的效果
// 说明一个Future对应当前执行完的callable任务
}
}
从打印结果来看,CompletionService完全解决了Future阻塞的特性,使用CompletionService接口后,哪个任务先执行完,哪个任务的返回值就先打印。
在CompletionService接口中如果当前没有任务被执行完,则completionService.take().get()方法还是呈阻塞特性。将上面的最后循环次数改成6次就阻塞了。
使用take()方法
方法take()取得最先完成任务的future对象,谁执行的时间最短谁就最先返回。
使用poll()方法
方法poll的作用是获取并移除表示下一个已完成任务的future,如果不存在这样的任务,则返回null,方法poll无阻塞的效果。
package cn.java.Concurrency.completionService;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author 小石潭记
* @date 2021/12/19 19:37
* @Description: ${todo}
*/
public class Run {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<>(executorService);
for (int i = 0; i < 1; i++) {
completionService.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
Thread.sleep(3000);
System.out.println("3秒过后了");
return "小石潭记 3s";
}
});
}
for (int i = 0; i < 1; i++) {
System.out.println(completionService.poll());
}
}
}
从运行结果来看,方法poll返回future为null,因为当前没有任何已经完成任务的future对象,返回为null,通过此实验证明poll方法不像take方法具有阻塞的效果。