1.CompletionService是什么?
答 :CompletionService的功能是以异步的方式,一边产生新任务,一边处理已完成的任务的结果,这样可以将执行任务与处理任务分开来处理。使用submit来执行任务,使用take来取得已完成的任务,并按照完成这些任务的时间顺序处理完成的任务。该接口其实与future的功能相似,但是与future的最大不同在于future必须要等待所有的任务处理完了才返回结果,而completionService能一边处理一边返回(先处理完的先返回)。
2.实例
package com.springboot.thread.completionService;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class TestCompletionService {
public static void main(String[] args) {
MyCallable myCallable1 = new MyCallable("userName1",1000);
MyCallable myCallable2 = new MyCallable("userName2",2000);
MyCallable myCallable3 = new MyCallable("userName3",3000);
MyCallable myCallable4 = new MyCallable("userName4",4000);
MyCallable myCallable5 = new MyCallable("userName5",5000);
MyCallable myCallable6 = new MyCallable("userName6",6000);
List<Callable> callableList = new ArrayList<>();
callableList.add(myCallable1);
callableList.add(myCallable2);
callableList.add(myCallable3);
callableList.add(myCallable4);
callableList.add(myCallable5);
callableList.add(myCallable6);
ThreadPoolExecutor executor = new ThreadPoolExecutor(6,10,5, TimeUnit.SECONDS,new LinkedBlockingDeque<>());
CompletionService 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)+"返回值");
try {
System.out.println(completionService.take().get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
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("i am " +userName);
Thread.sleep(sleepValue);
return "return "+userName;
}
}
运行结果:
i am userName2
i am userName1
i am userName3
i am userName4
i am userName5
等待打印第1返回值
i am userName6
return userName1
等待打印第2返回值
return userName2
等待打印第3返回值
return userName3
等待打印第4返回值
return userName4
等待打印第5返回值
return userName5
等待打印第6返回值
return userName6
从结果我们可以看出,返回的顺序性,同时take()方法具有阻塞性(当我们调用take方法的时候会获取已完成的future任务,如果没有已完成的则一直等待)。验证如下:
package com.springboot.thread.completionService;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class TestCompletionService {
public static void main(String[] args) {
MyCallable myCallable1 = new MyCallable("userName1",1000);
MyCallable myCallable2 = new MyCallable("userName2",2000);
MyCallable myCallable3 = new MyCallable("userName3",3000);
MyCallable myCallable4 = new MyCallable("userName4",4000);
MyCallable myCallable5 = new MyCallable("userName5",5000);
MyCallable myCallable6 = new MyCallable("userName6",6000);
List<Callable> callableList = new ArrayList<>();
callableList.add(myCallable1);
callableList.add(myCallable2);
callableList.add(myCallable3);
callableList.add(myCallable4);
callableList.add(myCallable5);
callableList.add(myCallable6);
ThreadPoolExecutor executor = new ThreadPoolExecutor(6,10,5, TimeUnit.SECONDS,new LinkedBlockingDeque<>());
CompletionService completionService = new ExecutorCompletionService(executor);
for (int i=0;i<=5;i++){
completionService.submit(callableList.get(i));
}
for (int i=0;i<=6;i++){
System.out.println("等待打印第"+(i+1)+"返回值");
try {
System.out.println(completionService.take().get());
//System.out.println(completionService.poll().get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
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("i am " +userName);
Thread.sleep(sleepValue);
return "return "+userName;
}
}
运行结果:
处于一直等待的状态。
3.使用poll()方法:获取并移除表示下一个已完成任务的Future,如果不存在这样的任务则返回null,次方法没有阻塞性。
Future<V> poll();
Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;(使用这个方法会先尝试去获取并移除已完成的Future如果不存在则会等待相应的时间)
4.CompletionService异常处理(待续)
5.CompletionService的submit
Future<V> submit(Callable<V> task);
Future<V> submit(Runnable task, V result);