Java 线程池-Executor

Executor

  1. Executor 是一个接口,已知的子接口为 ExecutorService, ScheduledExecutorService,
  2. 已知的实现类有AbstractExecutorService, ForkJoinPool, ScheduledThreadPoolExecutor, ThreadPoolExecutor,其中后三个类,会经常使用
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 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:
// 然而,Executor 并不严格的要求执行必须是异步的,一个简单的例子,这面这个执行器会在调用者的线程立即执行

 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. 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.
// 在包中,提供了 Executor 的实现 ExecutorService,ExecutorService提供了更多的实现, ThreadPoolExecutor 这个类就扩展了线程池的实现,Executors类提供了方便的工厂方法给这个执行器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值