Most visited

Recently visited

Added in API level 1

ExecutorCompletionService

public class ExecutorCompletionService
extends Object implements CompletionService<V>

java.lang.Object
   ↳ java.util.concurrent.ExecutorCompletionService<V>


CompletionService使用提供的Executor执行任务。 本课程安排提交的任务在完成后放置在可使用take访问的队列中。 该类轻量级足以适用于处理任务组时的瞬时使用。

用法示例。 假设你有一套针对某个问题的求解器,每个都返回一个类型为Result的值,并且想要并发地运行它们,在一些方法use(Result r)处理每个返回非空值的use(Result r) 你可以这样写:

 void solve(Executor e,
            Collection<Callable<Result>> solvers)
     throws InterruptedException, ExecutionException {
   CompletionService<Result> ecs
       = new ExecutorCompletionService<Result>(e);
   for (Callable<Result> s : solvers)
     ecs.submit(s);
   int n = solvers.size();
   for (int i = 0; i < n; ++i) {
     Result r = ecs.take().get();
     if (r != null)
       use(r);
   }
 }
Suppose instead that you would like to use the first non-null result of the set of tasks, ignoring any that encounter exceptions, and cancelling all other tasks when the first one is ready:
 void solve(Executor e,
            Collection<Callable<Result>> solvers)
     throws InterruptedException {
   CompletionService<Result> ecs
       = new ExecutorCompletionService<Result>(e);
   int n = solvers.size();
   List<Future<Result>> futures = new ArrayList<>(n);
   Result result = null;
   try {
     for (Callable<Result> s : solvers)
       futures.add(ecs.submit(s));
     for (int i = 0; i < n; ++i) {
       try {
         Result r = ecs.take().get();
         if (r != null) {
           result = r;
           break;
         }
       } catch (ExecutionException ignore) {}
     }
   }
   finally {
     for (Future<Result> f : futures)
       f.cancel(true);
   }

   if (result != null)
     use(result);
 }

Summary

Public constructors

ExecutorCompletionService(Executor executor)

使用提供的执行器为基础任务执行创建一个ExecutorCompletionService,并将 LinkedBlockingQueue作为完成队列创建。

ExecutorCompletionService(Executor executor, BlockingQueue<Future<V>> completionQueue)

使用提供的执行器为基础任务执行创建一个ExecutorCompletionService,并将提供的队列作为其完成队列。

Public methods

Future<V> poll(long timeout, TimeUnit unit)

检索并删除表示下一个已完成任务的Future,如果需要,则等待至指定的等待时间(如果没有任何时间存在)。

Future<V> poll()

检索并删除表示下一个已完成任务的未来,或者如果没有任何任务存在, null

Future<V> submit(Runnable task, V result)

提交可执行的任务并返回表示该任务的Future。

Future<V> submit(Callable<V> task)

提交执行的返回值任务,并返回表示未完成任务结果的Future。

Future<V> take()

检索并删除表示下一个已完成任务的Future,如果尚未存在,则等待。

Inherited methods

From class java.lang.Object
From interface java.util.concurrent.CompletionService

Public constructors

ExecutorCompletionService

Added in API level 1
ExecutorCompletionService (Executor executor)

使用提供的执行器为基础任务执行创建一个ExecutorCompletionService,并将 LinkedBlockingQueue作为完成队列创建。

Parameters
executor Executor: the executor to use
Throws
NullPointerException if executor is null

ExecutorCompletionService

Added in API level 1
ExecutorCompletionService (Executor executor, 
                BlockingQueue<Future<V>> completionQueue)

使用提供的执行器为基础任务执行创建一个ExecutorCompletionService,并将提供的队列作为其完成队列。

Parameters
executor Executor: the executor to use
completionQueue BlockingQueue: the queue to use as the completion queue normally one dedicated for use by this service. This queue is treated as unbounded -- failed attempted Queue.add operations for completed tasks cause them not to be retrievable.
Throws
NullPointerException if executor or completionQueue are null

Public methods

poll

Added in API level 1
Future<V> poll (long timeout, 
                TimeUnit unit)

检索并删除表示下一个已完成任务的Future,如果需要,则等待至指定的等待时间(如果没有任何时间存在)。

Parameters
timeout long: how long to wait before giving up, in units of unit
unit TimeUnit: a TimeUnit determining how to interpret the timeout parameter
Returns
Future<V> the Future representing the next completed task or null if the specified waiting time elapses before one is present
Throws
InterruptedException

poll

Added in API level 1
Future<V> poll ()

检索并删除表示下一个完成的任务的Future,或者如果没有任何任务存在,则表示 null

Returns
Future<V> the Future representing the next completed task, or null if none are present

submit

Added in API level 1
Future<V> submit (Runnable task, 
                V result)

提交可执行的任务并返回表示该任务的Future。 完成后,可以采取或调查此任务。

Parameters
task Runnable: the task to submit
result V: the result to return upon successful completion
Returns
Future<V> a Future representing pending completion of the task, and whose get() method will return the given result value upon completion

submit

Added in API level 1
Future<V> submit (Callable<V> task)

提交执行的返回值任务,并返回表示未完成任务结果的Future。 完成后,可以采取或调查此任务。

Parameters
task Callable: the task to submit
Returns
Future<V> a Future representing pending completion of the task

take

Added in API level 1
Future<V> take ()

检索并删除表示下一个已完成任务的Future,如果尚未存在,则等待。

Returns
Future<V> the Future representing the next completed task
Throws
InterruptedException

Hooray!