1、Executor接口
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 {@code Executor} is normally used instead of explicitly creating threads. For example, rather than invoking {@code new Thread(new(RunnableTask())).start()} for each of a set of tasks
executor框架是jdk1.5时引入的一个接口,主要目的是解耦任务的创建和任务的执行,在jdk1.5之前,我们用线程创建一个任务时,通常是这样 new Thread(new(RunnableTask())).start() ,当引入executor后我们这样来创建执行任务:
Executor executor = anExecutor;
executor.execute(new RunnableTask1());
executor.execute(new RunnableTask2());
但由于executor接口只定义了方法void execute(Runnable command) 而没有定义具体的实现,因而对于executor的不同实现,execute可能是创建一个新的线程并立即启动,有可能是使用已有的工作线程运行,或者可能将任务放入等待队列等待可用的工作线程。比如:
-
同步执行
class DirectExecutor implements Executor { public void execute(Runnable r) { r.run(); } }}
-
异步执行
class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); } }}
-
排队执行
class SerialExecutor implements Executor { final Queue
tasks = new ArrayDeque (); 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); } } }}
2、ExecutorService接口
除了继承Executor接口的功能外,还提供了关闭执行器的方法,更加通用的submit方法(除了可以接收runnable接口任务还可以接收callable接口任务,使用callable接口任务通常是需要获取执行结果的任务,它通过返回的Future来获取callable任务的执行结果)和批量运行Callable接口任务。
3、ScheduledExecutorService接口
除了继承ExecutorService接口功能外,还提供了延时执行和间隔执行的功能(scheduleWithFixedDelay,scheduleAtFixedRate)
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}}
4、Executors 工厂
对于上述3个接口,jdk1.5 都提供了默认的实现,但是如果用户自己去创建这些个默认实现的实例,就必须要了解这些默认实例的实现细节,而Executors 相当于就是一个简单工厂,通过提供一些简单的参数就可以创建出来我们想要的执行器。Executors为我们提供了五类执行器的创建:
-
创建固定线程数的Executor,返回ThreadPoolExecutor类型实例
public static ExecutorService newFixedThreadPool(int nThreads) public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)
-
单个线程的Executor,返回FinalizableDelegatedExecutorService或DelegatedScheduledExecutorService类型实例
public static ExecutorService newSingleThreadExecutor() public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) public static ScheduledExecutorService newSingleThreadScheduledExecutor() public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory)
-
可缓存的Executor
public static ExecutorService newCachedThreadPool() public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)
-
延时、周期性的Executor
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)
-
fork/join Executor,返回ForkJoinPool类实例
public static ExecutorService newWorkStealingPool(int parallelism)//并行级别 public static ExecutorService newWorkStealingPool()