Executor的使用流程为:
- 主线程首先创建实现Runnable或者Callable接口的任务对象。工具类Executors可以把Runnable对象封装成一个Callable对象(Executors.callable(Runnable task) 或者 Executors.callable(Runnable task, Object result));
- 把Runnable对象直接交给ExecutorService执行(ExecutorSevice.execute(Runnable command));或者把Runnable对象或Callable对象交给ExecutorService执行(ExecutorService.submit(Runnable task)或ExecutorService.submit(callable task));
- 如果执行ExecutorService.submit(...),将返回一个实现Future接口的对象(目前JDK中,返回的是FutureTask对象)。由于FutureTask对象实现了Runnable,也可直接创建FutureTask,然后交给ExecutorService执行;
- 最后,主线程可以执行Future.get()方法来等待任务的执行返回结果。也可以执行FutureTask.cancel(boolean mayInterruptIfRunnable)来取消此任务的执行。
并行计算求和
public class ConcurrentSum {
private int coreCpuNum;
private ExecutorService executor;
private List<FutureTask<Long>> tasks = new ArrayList<FutureTask<Long>>();
public ConcurrentSum(){
coreCpuNum = Runtime.getRuntime().availableProcessors();
executor = Executors.newFixedThreadPool(coreCpuNum);
}
class SumCalculator implements Callable<Long>{
int nums[];
int start;
int end;
public SumCalculator(final int nums[],int start,int end){
this.nums = nums;
this.start = start;
this.end = end;
}
@Override
public Long call() throws Exception {
long sum =0;
for(int i=start;i<end;i++){
sum += nums[i];
}
return sum;
}
}
public long sum(int[] nums){
int start,end,increment;
// 根据CPU核心个数拆分任务,创建FutureTask并提交到Executor
for(int i=0;i<coreCpuNum;i++){
increment = nums.length / coreCpuNum+1;
start = i*increment;
end = start+increment;
if(end > nums.length){
end = nums.length;
}
SumCalculator calculator = new SumCalculator(nums, start, end);
FutureTask<Long> task = new FutureTask<Long>(calculator);
tasks.add(task);
if(!executor.isShutdown()){
executor.submit(task);
}
}
return getPartSum();
}
public long getPartSum(){
long sum = 0;
for(int i=0;i<tasks.size();i++){
try {
sum += tasks.get(i).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return sum;
}
public void close(){
executor.shutdown();
}
public static void main(String[] args) {
int arr[] = new int[]{1, 22, 33, 4, 52, 61, 7, 48, 10, 11 };
long sum = new ConcurrentSum().sum(arr);
System.out.println("sum: " + sum);
}
}
CompletionService
在上述例子中,getResult()方法的实现过程中,迭代了FutureTask的数组,如果任务还没有完成则当前线程会阻塞,如果我们希望任意任务完成后就把其结果加到result中,而不用依次等待每个任务完成,可以使用CompletionService。
它与ExecutorService最主要的区别在于submit的task不一定是按照加入时的顺序完成的。CompletionService对ExecutorService进行了包装,内部维护一个保存Future对象的BlockingQueue。只有当这个Future对象状态是结束的时候,才会加入到这个Queue中,take()方法其实就是Producer-Consumer中的Consumer。它会从Queue中取出Future对象,如果Queue是空的,就会阻塞在那里,直到有完成的Future对象加入到Queue中。所以,先完成的必定先被取出。这样就减少了不必要的等待时间。
实例:并行计算求和
public class ConcurrentSum2 {
private int coreCpuNum;
private ExecutorService executor;
private CompletionService<Long> completionService;
public ConcurrentSum2(){
//.....
}
class SumCalculator implements Callable<Long>{
//.....
}
public long sum(int[] nums){
int start,end,increment;
// 根据CPU核心个数拆分任务,创建FutureTask并提交到Executor
for(int i=0;i<coreCpuNum;i++){
increment = nums.length / coreCpuNum+1;
start = i*increment;
end = start+increment;
if(end > nums.length){
end = nums.length;
}
SumCalculator task = new SumCalculator(nums, start, end);
if(!executor.isShutdown()){
completionService.submit(task);
}
}
return getPartSum();
}
public long getPartSum(){
long sum = 0;
for(int i=0;i<coreCpuNum;i++){
try {
sum += completionService.take().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return sum;
}
public void close(){
executor.shutdown();
}
}