Most visited

Recently visited

Added in API level 1

AbstractExecutorService

public abstract class AbstractExecutorService
extends Object implements ExecutorService

java.lang.Object
   ↳ java.util.concurrent.AbstractExecutorService
Known Direct Subclasses
Known Indirect Subclasses


提供ExecutorService执行方法的默认实现。 此类实现submitinvokeAnyinvokeAll使用方法RunnableFuture通过返回newTaskFor ,其默认为FutureTask类此包中提供。 例如,执行submit(Runnable)会创建一个执行并返回的关联RunnableFuture 子类可以覆盖newTaskFor方法以返回除FutureTask以外的其他RunnableFuture实现。

扩展示例 下面是一个定制ThreadPoolExecutor以使用CustomTask类代替默认FutureTask类的FutureTask

 public class CustomThreadPoolExecutor extends ThreadPoolExecutor {

   static class CustomTask<V> implements RunnableFuture<V> {...}

   protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
       return new CustomTask<V>(c);
   }
   protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
       return new CustomTask<V>(r, v);
   }
   // ... add constructors, etc.
 }

Summary

Public constructors

AbstractExecutorService()

Public methods

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)

执行给定的任务,返回一份持有其状态和结果的期货清单,当全部完成时。

<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)

执行给定的任务,当所有完成或超时到期时返回一份持有其状态和结果的期货清单,以先发生者为准。

<T> T invokeAny(Collection<? extends Callable<T>> tasks)

执行给定的任务,返回已成功完成的任务(即,没有抛出异常)的结果,如果有的话。

<T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)

执行给定的任务,返回已成功完成的任务(即,没有抛出异常)的结果,如果在给定超时过去之前执行了任务。

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

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

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

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

Future<?> submit(Runnable task)

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

Protected methods

<T> RunnableFuture<T> newTaskFor(Runnable runnable, T value)

针对给定的可运行和默认值返回 RunnableFuture

<T> RunnableFuture<T> newTaskFor(Callable<T> callable)

为给定的可调用任务返回一个 RunnableFuture

Inherited methods

From class java.lang.Object
From interface java.util.concurrent.ExecutorService
From interface java.util.concurrent.Executor

Public constructors

AbstractExecutorService

Added in API level 1
AbstractExecutorService ()

Public methods

invokeAll

Added in API level 9
List<Future<T>> invokeAll (Collection<? extends Callable<T>> tasks)

执行给定的任务,返回一份持有其状态和结果的期货清单,当全部完成时。 isDone()对于返回列表的每个元素都是true 请注意, 完成的任务可能正常结束或通过抛出异常终止。 如果在进行此操作时修改了给定集合,则此方法的结果未定义。

Parameters
tasks Collection: the collection of tasks
Returns
List<Future<T>> a list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list, each of which has completed
Throws
InterruptedException

invokeAll

Added in API level 9
List<Future<T>> invokeAll (Collection<? extends Callable<T>> tasks, 
                long timeout, 
                TimeUnit unit)

执行给定的任务,当所有完成或超时到期时返回一份持有其状态和结果的期货清单,以先发生者为准。 对于返回列表的每个元素, isDone()true 返回后,尚未完成的任务将被取消。 请注意, 完成的任务可能正常结束或通过抛出异常终止。 如果在进行此操作时修改了给定集合,则此方法的结果未定义。

Parameters
tasks Collection: the collection of tasks
timeout long: the maximum time to wait
unit TimeUnit: the time unit of the timeout argument
Returns
List<Future<T>> a list of Futures representing the tasks, in the same sequential order as produced by the iterator for the given task list. If the operation did not time out, each task will have completed. If it did time out, some of these tasks will not have completed.
Throws
InterruptedException

invokeAny

Added in API level 9
T invokeAny (Collection<? extends Callable<T>> tasks)

执行给定的任务,返回已成功完成的任务(即,没有抛出异常)的结果,如果有的话。 在正常或异常返回时,尚未完成的任务将被取消。 如果在进行此操作时修改了给定集合,则此方法的结果未定义。

Parameters
tasks Collection: the collection of tasks
Returns
T the result returned by one of the tasks
Throws
InterruptedException
ExecutionException

invokeAny

Added in API level 9
T invokeAny (Collection<? extends Callable<T>> tasks, 
                long timeout, 
                TimeUnit unit)

执行给定的任务,返回已成功完成的任务(即,没有抛出异常)的结果,如果在给定超时过去之前执行了任务。 在正常或异常返回时,尚未完成的任务将被取消。 如果在进行此操作时修改了给定集合,则此方法的结果未定义。

Parameters
tasks Collection: the collection of tasks
timeout long: the maximum time to wait
unit TimeUnit: the time unit of the timeout argument
Returns
T the result returned by one of the tasks
Throws
InterruptedException
ExecutionException
TimeoutException

submit

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

提交执行的返回值任务,并返回表示未完成任务结果的Future。 未来的get方法将在成功完成时返回任务的结果。

如果您想立即阻止等待任务,可以使用表单 result = exec.submit(aCallable).get();

注意: Executors类包含一组方法,可以将一些其他常见的闭包对象(例如, PrivilegedActionCallable表单,以便它们可以提交。

Parameters
task Callable: the task to submit
Returns
Future<T> a Future representing pending completion of the task
Throws
RejectedExecutionException
NullPointerException

submit

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

提交可执行的任务并返回表示该任务的Future。 未来的get方法将在成功完成后返回给定的结果。

Parameters
task Runnable: the task to submit
result T: the result to return
Returns
Future<T> a Future representing pending completion of the task
Throws
RejectedExecutionException
NullPointerException

submit

Added in API level 1
Future<?> submit (Runnable task)

提交可执行的任务并返回表示该任务的Future。 成功完成后,未来的get方法将返回null

Parameters
task Runnable: the task to submit
Returns
Future<?> a Future representing pending completion of the task
Throws
RejectedExecutionException
NullPointerException

Protected methods

newTaskFor

Added in API level 9
RunnableFuture<T> newTaskFor (Runnable runnable, 
                T value)

针对给定的可运行和默认值返回 RunnableFuture

Parameters
runnable Runnable: the runnable task being wrapped
value T: the default value for the returned future
Returns
RunnableFuture<T> a RunnableFuture which, when run, will run the underlying runnable and which, as a Future, will yield the given value as its result and provide for cancellation of the underlying task

newTaskFor

Added in API level 9
RunnableFuture<T> newTaskFor (Callable<T> callable)

为给定的可调用任务返回一个 RunnableFuture

Parameters
callable Callable: the callable task being wrapped
Returns
RunnableFuture<T> a RunnableFuture which, when run, will call the underlying callable and which, as a Future, will yield the callable's result as its result and provide for cancellation of the underlying task

Hooray!