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後咱們這樣來建立執行任務:java
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(); } }}
排隊執行ide
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); } } }}
除了繼承Executor接口的功能外,還提供了關閉執行器的方法,更加通用的submit方法(除了能夠接收runnable接口任務還能夠接收callable接口任務,使用callable接口任務一般是須要獲取執行結果的任務,它經過返回的Future來獲取callable任務的執行結果)和批量運行Callable接口任務。this
除了繼承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); } }}
對於上述3個接口,jdk1.5 都提供了默認的實現,可是若是用戶本身去建立這些個默認實現的實例,就必需要了解這些默認實例的實現細節,而Executors 至關於就是一個簡單工廠,經過提供一些簡單的參數就能夠建立出來咱們想要的執行器。Executors爲咱們提供了五類執行器的建立:code
建立固定線程數的Executor,返回ThreadPoolExecutor類型實例orm
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()