Executor

All Known Subinterfaces:
ExecutorService, ScheduledExecutorService
All Known Implementing Classes:
AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor


public interface Executor

An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run(该界面提供了一种将任务提交与每个任务如何运行的机制解耦的方法), including details of thread use, scheduling(调度), etc(等等). An Executor is normally used instead(代替) of explicitly(明确) creating threads. For example, rather than(而不是) invoking new Thread(new(RunnableTask())).start() for each of a set of tasks(而不是为一组任务调用新的), you might use:

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

However, the Executor interface does not strictly(严格) require(要求) that execution be asynchronous(异步). In the simplest(简单) case, an executor can run the submitted task immediately(立即) in the caller’s thread:

 class DirectExecutor implements Executor {
   public void execute(Runnable r) {
     r.run();
   }
 }

More typically, tasks are executed in some thread other than the caller’s thread. The executor below(下面) spawns(大量生产) a new thread for each task.

 class ThreadPerTaskExecutor implements Executor {
   public void execute(Runnable r) {
     new Thread(r).start();
   }
 }

Many Executor implementations impose(强加) some sort(某些,某种) of limitation(限制) on how and when tasks are scheduled(任务如何和何时安排). The executor below serializes(连续) the submission of tasks to a second executor, illustrating(说明) a composite(合成) executor(说明复合执行器。).

 class SerialExecutor implements Executor {
   final Queue<Runnable> tasks = new ArrayDeque<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(active);
     }
   }
 }

The Executor implementations provided in this package implement ExecutorService, which is a more extensive interface(这是一个更广泛的Interface). The ThreadPoolExecutor class provides an extensible(扩展) thread pool implementation. The Executors class provides convenient(方便) factory methods for these Executors.
Memory consistency(一致性) effects(效应): Actions in a thread prior(预先) to submitting a Runnable object to an Executor happen-before(发生-前) its execution begins, perhaps(或者) in another thread.

Since:
1.5

Method Detail

execute

void execute(Runnable command)

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.
Parameters:
command - the runnable task
Throws:
RejectedExecutionException - if this task cannot be accepted for execution
NullPointerException - if command is null

Executor框架是Java中的一个重要概念,它主要用于异步执行任务。这个框架的主要功能是将任务的提交与任务的执行分离开来,提供了线程池、延迟和定时执行等功能。 Executor框架的主要组成部分包括以下几个接口和类: 1. Executor:这是最基本的执行器接口,它定义了一个方法execute(Runnable command),用于执行给定的任务。 2. ExecutorService:这是Executor接口的子接口,它增加了一些管理终止和产生Future的方法。 3. ThreadPoolExecutor:这是ExecutorService接口的一个实现,它创建了一个线程池来执行任务。 4. ScheduledExecutorService:这是ExecutorService接口的一个子接口,它支持定期或延迟执行任务。 5. Executors:这是一个工具类,它提供了一些工厂方法,用于创建各种类型的执行器。 6. Future和Callable:Future是一个接口,代表一个异步计算的结果。Callable是一个接口,它的call()方法返回一个结果,并且可以抛出异常。 7. FutureTask:这是一个类,它是Runnable和Future接口的实现,它可以包装Callable或者Runnable对象。 使用Executor框架的好处主要有以下几点: 1. 提高性能:通过重复利用已存在的线程来降低线程创建和销毁造成的开销。 2. 提高响应性:当命令到达时,线程可以立即运行,而不用等待其他线程。 3. 提高线程的可管理性:可以根据需要对线程进行调度、优先级设置等操作。 在使用时,需要注意以下几点: 1. 合理选择线程池的大小:过大的线程池会占用过多的系统资源,过小的线程池则无法充分利用多核CPU的优势。 2. 注意线程安全:在使用多线程时,需要注意数据的同步问题,避免出现数据不一致的情况。 3. 正确处理异常:在异步任务中,如果发生异常,需要有合适的机制进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值