|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjava.util.concurrent.AbstractExecutorService
public abstract class AbstractExecutorService
提供 ExecutorService 执行方法的默认实现。此类使用 newTaskFor 返回的 RunnableFuture 实现 submit、invokeAny 和 invokeAll 方法,默认情况下,RunnableFuture 是此包中提供的 FutureTask 类。例如,submit(Runnable) 的实现创建了一个关联 RunnableFuture 类,该类将被执行并返回。子类可以重写 newTaskFor 方法,以返回 FutureTask 之外的 RunnableFuture 实现。
扩展示例。以下是一个类的简要介绍,该类定制 ThreadPoolExecutor 使用 CustomTask 类替代默认 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.
}
| 构造方法摘要 | |
|---|---|
AbstractExecutorService()
|
|
| 方法摘要 | ||
|---|---|---|
|
invokeAll(Collection<? extends Callable<T>> tasks)
执行给定的任务,当所有任务完成时,返回保持任务状态和结果的 Future 列表。 |
|
|
invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
执行给定的任务,当所有任务完成或超时期满时(无论哪个首先发生),返回保持任务状态和结果的 Future 列表。 |
|
|
invokeAny(Collection<? extends Callable<T>> tasks)
执行给定的任务,如果某个任务已成功完成(也就是未抛出异常),则返回其结果。 |
|
|
invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
执行给定的任务,如果在给定的超时期满前某个任务已成功完成(也就是未抛出异常),则返回其结果。 |
|
protected
|
newTaskFor(Callable<T> callable)
为给定可调用任务返回一个 RunnableFuture。 |
|
protected
|
newTaskFor(Runnable runnable,
T value)
为给定可运行任务和默认值返回一个 RunnableFuture。 |
|
|
submit(Callable<T> task)
提交一个返回值的任务用于执行,返回一个表示任务的未决结果的 Future。 |
|
Future<?> |
submit(Runnable task)
提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。 |
|
|
submit(Runnable task,
T result)
提交一个 Runnable 任务用于执行,并返回一个表示该任务的 Future。 |
|
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| 从接口 java.util.concurrent.ExecutorService 继承的方法 |
|---|
awaitTermination, isShutdown, isTerminated, shutdown, shutdownNow |
| 从接口 java.util.concurrent.Executor 继承的方法 |
|---|
execute |
| 构造方法详细信息 |
|---|
public AbstractExecutorService()
| 方法详细信息 |
|---|
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable,
T value)
runnable - 将被包装的可运行任务value - 用于所返回的将来任务的默认值
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable)
callable - 将包装的可调用任务
public Future<?> submit(Runnable task)
ExecutorService 复制的描述
ExecutorService 中的 submittask - 要提交的任务
public <T> Future<T> submit(Runnable task,
T result)
ExecutorService 复制的描述
ExecutorService 中的 submittask - 要提交的任务result - 返回的结果
public <T> Future<T> submit(Callable<T> task)
ExecutorService 复制的描述如果想立即阻塞任务的等待,则可以使用 result = exec.submit(aCallable).get(); 形式的构造。
注:Executors 类包括了一组方法,可以转换某些其他常见的类似于闭包的对象,例如,将 PrivilegedAction 转换为 Callable 形式,这样就可以提交它们了。
ExecutorService 中的 submittask - 要提交的任务
public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException,
ExecutionException
ExecutorService 复制的描述
ExecutorService 中的 invokeAnytasks - 任务 collection
InterruptedException - 如果等待时发生中断
ExecutionException - 如果没有任务成功完成
public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
throws InterruptedException,
ExecutionException,
TimeoutException
ExecutorService 复制的描述
ExecutorService 中的 invokeAnytasks - 任务 collectiontimeout - 最长等待时间unit - timeout 参数的时间单位
InterruptedException - 如果等待时发生中断
ExecutionException - 如果没有任务成功完成
TimeoutException - 如果在所有任务成功完成之前给定的超时期满
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException
ExecutorService 复制的描述Future.isDone() 为 true。注意,可以正常地或通过抛出异常来终止已完成 任务。如果正在进行此操作时修改了给定的 collection,则此方法的结果是不确定的。
ExecutorService 中的 invokeAlltasks - 任务 collection
InterruptedException - 如果等待时发生中断,在这种情况下取消尚未完成的任务。
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
throws InterruptedException
ExecutorService 复制的描述Future.isDone() 为 true。一旦返回后,即取消尚未完成的任务。注意,可以正常地或通过抛出异常来终止已完成 任务。如果此操作正在进行时修改了给定的 collection,则此方法的结果是不确定的。
ExecutorService 中的 invokeAlltasks - 任务 collectiontimeout - 最长等待时间unit - timeout 参数的时间单位
InterruptedException - 如果等待时发生中断,在这种情况下取消尚未完成的任务
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。