搜档网
当前位置:搜档网 › Executor

Executor

Executor (Java 2 Platform SE 5.0)
function windowTitle()
{
parent.document.title="Executor (Java 2 Platform SE 5.0)";
}

概述 
软件包 
 类 
使用 
树 
已过时 
索引 
帮助 

JavaTM 2 PlatformStandard Ed. 5.0
 上一个类 
 下一个类
框架  
 无框架  
 
!--
if(window==top) {
document.writeln('所有类');
}
//--
所有类
摘要: 嵌套 | 字段 | 构造方法 | 方法
详细信息: 字段 | 构造方法 | 方法
java.util.concurrent
接口 Executor
所有已知子接口: ExecutorService, ScheduledExecutorService
所有已知实现类: AbstractExecutorService, ScheduledThreadPoolExecutor, ThreadPoolExecutor
public interface Executor
执行已提交的 Runnable 任务的对象。此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节、调度等)分离开来的方法。通常使用 Executor 而不是显式地创建线程。例如,可能会使用以下方法,而不是为一组任务中的每个任务调用 new Thread(new(RunnableTask())).start():

Executor executor = anExecutor;
executor.execute(new RunnableTask1());
executor.execute(new RunnableTask2());
...


不过,Executor 接口并没有严格地要求执行是异步的。在最简单的情况下,执行程序可以在调用方的线程中立即运行已提交的任务:

class DirectExecutor implements Executor {
public void execute(Runnable r) {
r.run();
}
}
更常见的是,任务是在某个不是调用方线程的线程中执行的。以下执行程序将为每个任务生成一个新线程。

class ThreadPerTaskExecutor implements Executor {
public void execute(Runnable r) {
new Thread(r).start();
}
}
许多 Executor 实现都对调度任务的方式和时间强加了某种限制。以下执行程序使任务提交与第二个执行程序保持连续,这说明了一个复合执行程序。

class SerialExecutor implements Executor {
final Queue<Runnable> tasks = new LinkedBlockingQueue<Runnable>();
final Executor executor;
Runnable active;
SerialExecutor(Executor executor) {
this.executor = executor;
}
public synchronized void execute(final Runnable r) {
tasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
scheduleNext();
}
}
});
if (active == null) {
scheduleNext();
}
}
protected synchronized void scheduleNext() {
if ((active = tasks.poll()) != null) {
executor.execute(acti

ve);
}
}
}
此包中提供的 Executor 实现实现了 ExecutorService,这是一个使用更广泛的接口。ThreadPoolExecutor 类提供一个可扩展的线程池实现。Executors 类为这些 Executor 提供了便捷的工厂方法。
从以下版本开始:
1.5
方法摘要
 void
execute(Runnable command)
          在未来某个时间执行给定的命令。
 
方法详细信息
execute
void execute(Runnable command)
在未来某个时间执行给定的命令。该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由 Executor 实现决定。
参数:command - 可运行的任务
抛出:
RejectedExecutionException - 如果不能接受执行此任务。
NullPointerException - 如果命令为 null

概述 
软件包 
 类 
使用 
树 
已过时 
索引 
帮助 

JavaTM 2 PlatformStandard Ed. 5.0
 上一个类 
 下一个类
框架  
 无框架  
 
!--
if(window==top) {
document.writeln('所有类');
}
//--
所有类
摘要: 嵌套 | 字段 | 构造方法 | 方法
详细信息: 字段 | 构造方法 | 方法
提交错误或意见有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。 版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。


相关主题