您好,登錄后才能下訂單哦!
本篇文章為大家展示了Java J.U.C中executors框架的設(shè)計(jì)理念是什么,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
juc-executors
框架是整個(gè)J.U.C包中類/接口關(guān)系最復(fù)雜的框架,真正理解executors框架的前提是理清楚各個(gè)模塊之間的關(guān)系,高屋建瓴,從整體到局部才能透徹理解其中各個(gè)模塊的功能和背后的設(shè)計(jì)思路。
網(wǎng)上有太多文章講executors框架,要么泛泛而談,要么一葉障目不見泰山,缺乏整體視角,很多根本沒有理解整個(gè)框架的設(shè)計(jì)思想和模塊關(guān)系。本文將對(duì)整個(gè)executors框架做綜述,介紹各個(gè)模塊的功能和聯(lián)系,后續(xù)再深入探討每個(gè)模塊,包括模塊中的各個(gè)工具類。
Executor
是JDK1.5時(shí),隨著J.U.C引入的一個(gè)接口,引入該接口的主要目的是解耦任務(wù)本身和任務(wù)的執(zhí)行。我們之前通過線程執(zhí)行一個(gè)任務(wù)時(shí),往往需要先創(chuàng)建一個(gè)線程,然后調(diào)用線程的start
方法來執(zhí)行任務(wù):
new Thread(new(RunnableTask())).start();
上述RunnableTask是實(shí)現(xiàn)了Runnable接口的任務(wù)類。而Executor接口解耦了任務(wù)和任務(wù)的執(zhí)行,該接口只有一個(gè)方法,入?yún)榇龍?zhí)行的任務(wù):
public interface Executor { /** * 執(zhí)行給定的Runnable任務(wù). * 根據(jù)Executor的實(shí)現(xiàn)不同, 具體執(zhí)行方式也不相同. * * @param command the runnable task * @throws RejectedExecutionException if this task cannot be accepted for execution * @throws NullPointerException if command is null */ void execute(Runnable command); }
我們可以像下面這樣執(zhí)行任務(wù),而不必關(guān)心線程的創(chuàng)建:
Executor executor = someExecutor; // 創(chuàng)建具體的Executor對(duì)象 executor.execute(new RunnableTask1()); executor.execute(new RunnableTask2()); ...
由于Executor僅僅是一個(gè)接口,所以根據(jù)其實(shí)現(xiàn)的不同,執(zhí)行任務(wù)的具體方式也不盡相同,比如:
①同步執(zhí)行任務(wù)
class DirectExecutor implements Executor { public void execute(Runnable r) { r.run(); } }
DirectExecutor是一個(gè)同步任務(wù)執(zhí)行器,對(duì)于傳入的任務(wù),只有執(zhí)行完成后execute才會(huì)返回。
②異步執(zhí)行任務(wù)
/** Fallback if ForkJoinPool.commonPool() cannot support parallelism */ static final class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); } }
ThreadPerTaskExecutor是一個(gè)異步任務(wù)執(zhí)行器,對(duì)于每個(gè)任務(wù),執(zhí)行器都會(huì)創(chuàng)建一個(gè)新的線程去執(zhí)行任務(wù)。
注意:Java線程與本地操作系統(tǒng)的線程是一一映射的。Java線程啟動(dòng)時(shí)會(huì)創(chuàng)建一個(gè)本地操作系統(tǒng)線程;當(dāng)該Java線程終止時(shí),對(duì)應(yīng)操作系統(tǒng)線程會(huì)被回收。由于CPU資源是有限的,所以線程數(shù)量有上限,所以一般由線程池來管理線程的創(chuàng)建/回收,而上面這種方式其實(shí)是線程池的雛形。
③對(duì)任務(wù)進(jìn)行排隊(duì)執(zhí)行
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); } } }
SerialExecutor 會(huì)對(duì)傳入的任務(wù)進(jìn)行排隊(duì)(FIFO順序),然后從隊(duì)首取出一個(gè)任務(wù)執(zhí)行。
以上這些示例僅僅是給出了一些可能的Executor實(shí)現(xiàn),J.U.C包中提供了很多Executor的具體實(shí)現(xiàn)類,我們以后會(huì)具體講到,這里關(guān)鍵是理解Executor的設(shè)計(jì)思想——對(duì)任務(wù)和任務(wù)的執(zhí)行解耦。
Executor接口提供的功能很簡單,為了對(duì)它進(jìn)行增強(qiáng),J.U.C又提供了一個(gè)名為ExecutorService
接口,ExecutorService也是在JDK1.5時(shí),隨著J.U.C引入的:
public interface ExecutorService extends Executor
可以看到,ExecutorService繼承了Executor,它在Executor的基礎(chǔ)上增強(qiáng)了對(duì)任務(wù)的控制,同時(shí)包括對(duì)自身生命周期的管理,主要有四類:
關(guān)閉執(zhí)行器,禁止任務(wù)的提交;
監(jiān)視執(zhí)行器的狀態(tài);
提供對(duì)異步任務(wù)的支持;
提供對(duì)批處理任務(wù)的支持。
public interface ExecutorService extends Executor { /** * 關(guān)閉執(zhí)行器, 主要有以下特點(diǎn): * 1. 已經(jīng)提交給該執(zhí)行器的任務(wù)將會(huì)繼續(xù)執(zhí)行, 但是不再接受新任務(wù)的提交; * 2. 如果執(zhí)行器已經(jīng)關(guān)閉了, 則再次調(diào)用沒有副作用. */ void shutdown(); /** * 立即關(guān)閉執(zhí)行器, 主要有以下特點(diǎn): * 1. 嘗試停止所有正在執(zhí)行的任務(wù), 無法保證能夠停止成功, 但會(huì)盡力嘗試(例如, 通過 Thread.interrupt中斷任務(wù), 但是不響應(yīng)中斷的任務(wù)可能無法終止); * 2. 暫停處理已經(jīng)提交但未執(zhí)行的任務(wù); * * @return 返回已經(jīng)提交但未執(zhí)行的任務(wù)列表 */ List<Runnable> shutdownNow(); /** * 如果該執(zhí)行器已經(jīng)關(guān)閉, 則返回true. */ boolean isShutdown(); /** * 判斷執(zhí)行器是否已經(jīng)【終止】. * <p> * 僅當(dāng)執(zhí)行器已關(guān)閉且所有任務(wù)都已經(jīng)執(zhí)行完成, 才返回true. * 注意: 除非首先調(diào)用 shutdown 或 shutdownNow, 否則該方法永遠(yuǎn)返回false. */ boolean isTerminated(); /** * 阻塞調(diào)用線程, 等待執(zhí)行器到達(dá)【終止】狀態(tài). * * @return {@code true} 如果執(zhí)行器最終到達(dá)終止?fàn)顟B(tài), 則返回true; 否則返回false * @throws InterruptedException if interrupted while waiting */ boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; /** * 提交一個(gè)具有返回值的任務(wù)用于執(zhí)行. * 注意: Future的get方法在成功完成時(shí)將會(huì)返回task的返回值. * * @param task 待提交的任務(wù) * @param <T> 任務(wù)的返回值類型 * @return 返回該任務(wù)的Future對(duì)象 * @throws RejectedExecutionException 如果任務(wù)無法安排執(zhí)行 * @throws NullPointerException if the task is null */ <T> Future<T> submit(Callable<T> task); /** * 提交一個(gè) Runnable 任務(wù)用于執(zhí)行. * 注意: Future的get方法在成功完成時(shí)將會(huì)返回給定的結(jié)果(入?yún)r(shí)指定). * * @param task 待提交的任務(wù) * @param result 返回的結(jié)果 * @param <T> 返回的結(jié)果類型 * @return 返回該任務(wù)的Future對(duì)象 * @throws RejectedExecutionException 如果任務(wù)無法安排執(zhí)行 * @throws NullPointerException if the task is null */ <T> Future<T> submit(Runnable task, T result); /** * 提交一個(gè) Runnable 任務(wù)用于執(zhí)行. * 注意: Future的get方法在成功完成時(shí)將會(huì)返回null. * * @param task 待提交的任務(wù) * @return 返回該任務(wù)的Future對(duì)象 * @throws RejectedExecutionException 如果任務(wù)無法安排執(zhí)行 * @throws NullPointerException if the task is null */ Future<?> submit(Runnable task); /** * 執(zhí)行給定集合中的所有任務(wù), 當(dāng)所有任務(wù)都執(zhí)行完成后, 返回保持任務(wù)狀態(tài)和結(jié)果的 Future 列表. * <p> * 注意: 該方法為同步方法. 返回列表中的所有元素的Future.isDone() 為 true. * * @param tasks 任務(wù)集合 * @param <T> 任務(wù)的返回結(jié)果類型 * @return 任務(wù)的Future對(duì)象列表,列表順序與集合中的迭代器所生成的順序相同, * @throws InterruptedException 如果等待時(shí)發(fā)生中斷, 會(huì)將所有未完成的任務(wù)取消. * @throws NullPointerException 任一任務(wù)為 null * @throws RejectedExecutionException 如果任一任務(wù)無法安排執(zhí)行 */ <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException; /** * 執(zhí)行給定集合中的所有任務(wù), 當(dāng)所有任務(wù)都執(zhí)行完成后或超時(shí)期滿時(shí)(無論哪個(gè)首先發(fā)生), 返回保持任務(wù)狀態(tài)和結(jié)果的 Future 列表. */ <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException; /** * 執(zhí)行給定集合中的任務(wù), 只有其中某個(gè)任務(wù)率先成功完成(未拋出異常), 則返回其結(jié)果. * 一旦正?;虍惓7祷睾? 則取消尚未完成的任務(wù). */ <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException; /** * 執(zhí)行給定集合中的任務(wù), 如果在給定的超時(shí)期滿前, 某個(gè)任務(wù)已成功完成(未拋出異常), 則返回其結(jié)果. * 一旦正常或異常返回后, 則取消尚未完成的任務(wù). */ <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; }
關(guān)于Future,其實(shí)就是Java多線程設(shè)計(jì)模式中 Future模式,后面我們會(huì)專門講解J.U.C中的Future框架。
Future
對(duì)象提供了對(duì)任務(wù)異步執(zhí)行的支持,也就是說調(diào)用線程無需等待任務(wù)執(zhí)行完成,提交待執(zhí)行的任務(wù)后,就會(huì)立即返回往下執(zhí)行。然后,可以在需要時(shí)檢查Future是否有結(jié)果了,如果任務(wù)已執(zhí)行完畢,通過Future.get()
方法可以獲取到執(zhí)行結(jié)果——Future.get()是阻塞方法。
在工業(yè)環(huán)境中,我們可能希望提交給執(zhí)行器的某些任務(wù)能夠定時(shí)執(zhí)行或周期性地執(zhí)行,這時(shí)我們可以自己實(shí)現(xiàn)Executor接口來創(chuàng)建符合我們需要的類,Doug Lea已經(jīng)考慮到了這類需求,所以在ExecutorService的基礎(chǔ)上,又提供了一個(gè)接口——ScheduledExecutorService
,該接口也是在JDK1.5時(shí),隨著J.U.C引入的:
public interface ScheduledExecutorService extends ExecutorService
ScheduledExecutorService提供了一系列schedule方法,可以在給定的延遲后執(zhí)行提交的任務(wù),或者每個(gè)指定的周期執(zhí)行一次提交的任務(wù),我們來看下面這個(gè)示例:
public class TestSche { public static void main(String[] args) { //創(chuàng)建了一個(gè)ScheduledExecutorService 實(shí)例 ScheduledExecutorService executorService=new ScheduledThreadPoolExecutor(1); final ScheduledFuture<?>scheduledFuture=executorService.scheduleAtFixedRate(new BeepTask(),10,10 ,TimeUnit.SECONDS); executorService.schedule(new Runnable() { @Override public void run() { scheduledFuture.cancel(true); } },1,TimeUnit.HOURS); //1小時(shí)取消任務(wù) } private static class BeepTask implements Runnable{ @Override public void run() { System.out.println("beep!..."); } } }
上述示例先創(chuàng)建一個(gè)ScheduledExecutorService類型的執(zhí)行器,然后利用scheduleAtFixedRate方法提交了一個(gè)“蜂鳴”任務(wù),每隔10s該任務(wù)會(huì)執(zhí)行一次。
注意:scheduleAtFixedRate
方法返回一個(gè)ScheduledFuture對(duì)象,ScheduledFuture其實(shí)就是在Future的基礎(chǔ)上增加了延遲的功能。通過ScheduledFuture,可以取消一個(gè)任務(wù)的執(zhí)行,本例中我們利用schedule方法,設(shè)定在1小時(shí)后,執(zhí)行任務(wù)的取消。
ScheduledExecutorService完整的接口聲明如下:
public interface ScheduledExecutorService extends ExecutorService { /** * 提交一個(gè)待執(zhí)行的任務(wù), 并在給定的延遲后執(zhí)行該任務(wù). * * @param command 待執(zhí)行的任務(wù) * @param delay 延遲時(shí)間 * @param unit 延遲時(shí)間的單位 */ public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); /** * 提交一個(gè)待執(zhí)行的任務(wù)(具有返回值), 并在給定的延遲后執(zhí)行該任務(wù). * * @param command 待執(zhí)行的任務(wù) * @param delay 延遲時(shí)間 * @param unit 延遲時(shí)間的單位 * @param <V> 返回值類型 */ public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); /** * 提交一個(gè)待執(zhí)行的任務(wù). * 該任務(wù)在 initialDelay 后開始執(zhí)行, 然后在 initialDelay+period 后執(zhí)行, 接著在 initialDelay + 2 * period 后執(zhí)行, 依此類推. * * @param command 待執(zhí)行的任務(wù) * @param initialDelay 首次執(zhí)行的延遲時(shí)間 * @param period 連續(xù)執(zhí)行之間的周期 * @param unit 延遲時(shí)間的單位 */ public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); /** * 提交一個(gè)待執(zhí)行的任務(wù). * 該任務(wù)在 initialDelay 后開始執(zhí)行, 隨后在每一次執(zhí)行終止和下一次執(zhí)行開始之間都存在給定的延遲. * 如果任務(wù)的任一執(zhí)行遇到異常, 就會(huì)取消后續(xù)執(zhí)行. 否則, 只能通過執(zhí)行程序的取消或終止方法來終止該任務(wù). * * @param command 待執(zhí)行的任務(wù) * @param initialDelay 首次執(zhí)行的延遲時(shí)間 * @param delay 一次執(zhí)行終止和下一次執(zhí)行開始之間的延遲 * @param unit 延遲時(shí)間的單位 */ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); }
至此,Executors框架中的三個(gè)最核心的接口介紹完畢,這三個(gè)接口的關(guān)系如下圖:
通過第一部分的學(xué)習(xí),讀者應(yīng)該對(duì)Executors框架有了一個(gè)初步的認(rèn)識(shí),Executors框架就是用來解耦任務(wù)本身與任務(wù)的執(zhí)行,并提供了三個(gè)核心接口來滿足使用者的需求:
Executor:提交普通的可執(zhí)行任務(wù)
ExecutorService:提供對(duì)線程池生命周期的管理、異步任務(wù)的支持
ScheduledExecutorService:提供對(duì)任務(wù)的周期性執(zhí)行支持
既然上面三種執(zhí)行器只是接口,那么就一定存在具體的實(shí)現(xiàn)類,J.U.C提供了許多默認(rèn)的接口實(shí)現(xiàn),如果要用戶自己去創(chuàng)建這些類的實(shí)例,就需要了解這些類的細(xì)節(jié),有沒有一種直接的方式,僅僅根據(jù)一些需要的特性(參數(shù))就創(chuàng)建這些實(shí)例呢?因?yàn)閷?duì)于用戶來說,其實(shí)使用的只是這三個(gè)接口。
JDK1.5時(shí),J.U.C中還提供了一個(gè)Executors
類,專門用于創(chuàng)建上述接口的實(shí)現(xiàn)類對(duì)象。Executors其實(shí)就是一個(gè)簡單工廠,它的所有方法都是static的,用戶可以根據(jù)需要,選擇需要?jiǎng)?chuàng)建的執(zhí)行器實(shí)例,Executors一共提供了五類可供創(chuàng)建的Executor執(zhí)行器實(shí)例。
Executors提供了兩種創(chuàng)建具有固定線程數(shù)的Executor的方法,固定線程池在初始化時(shí)確定其中的線程總數(shù),運(yùn)行過程中會(huì)始終維持線程數(shù)量不變。
可以看到下面的兩種創(chuàng)建方法其實(shí)都返回了一個(gè)ThreadPoolExecutor
實(shí)例。ThreadPoolExecutor是一個(gè)ExecutorService接口的實(shí)現(xiàn)類,我們會(huì)在后面用專門章節(jié)講解,現(xiàn)在只需要了解這是一種Executor,用來調(diào)度其中的線程的執(zhí)行即可。
/** * 創(chuàng)建一個(gè)具有固定線程數(shù)的Executor. */ public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } /** * 創(chuàng)建一個(gè)具有固定線程數(shù)的Executor. * 在需要時(shí)使用提供的 ThreadFactory 創(chuàng)建新線程. */ public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory); }
上面需要注意的是ThreadFactory
這個(gè)接口:
public interface ThreadFactory { Thread newThread(Runnable r); }
既然返回的是一個(gè)線程池,那么就涉及線程的創(chuàng)建,一般我們需要通過 new Thread ()
這種方法創(chuàng)建一個(gè)新線程,但是我們可能希望設(shè)置一些線程屬性,比如
名稱、守護(hù)程序狀態(tài)、ThreadGroup 等等,線程池中的線程非常多,如果每個(gè)線程都這樣手動(dòng)配置勢(shì)必非常繁瑣,而ThreadFactory 作為一個(gè)線程工廠可以讓我們從這些繁瑣的線程狀態(tài)設(shè)置的工作中解放出來,還可以由外部指定ThreadFactory實(shí)例,以決定線程的具體創(chuàng)建方式。
Executors提供了靜態(tài)內(nèi)部類,實(shí)現(xiàn)了ThreadFactory接口,最簡單且常用的就是下面這個(gè)DefaultThreadFactory :
/** * 默認(rèn)的線程工廠. */ static class DefaultThreadFactory implements ThreadFactory { private static final AtomicInteger poolNumber = new AtomicInteger(1); private final ThreadGroup group; private final AtomicInteger threadNumber = new AtomicInteger(1); private final String namePrefix; DefaultThreadFactory() { SecurityManager s = System.getSecurityManager(); group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-"; } public Thread newThread(Runnable r) { Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); if (t.isDaemon()) t.setDaemon(false); if (t.getPriority() != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY); return t; } }
可以看到,DefaultThreadFactory 初始化的時(shí)候定義了線程組、線程名稱等信息,每創(chuàng)建一個(gè)線程,都給線程統(tǒng)一分配這些信息,避免了一個(gè)個(gè)手工通過new的方式創(chuàng)建線程,又可進(jìn)行工廠的復(fù)用。
除了固定線程數(shù)的線程池,Executors還提供了兩種創(chuàng)建只有單個(gè)線程Executor的方法:
/** * 創(chuàng)建一個(gè)使用單個(gè) worker 線程的 Executor. */ public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); } /** * 創(chuàng)建一個(gè)使用單個(gè) worker 線程的 Executor. * 在需要時(shí)使用提供的 ThreadFactory 創(chuàng)建新線程. */ public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory)); }
可以看到,只有單個(gè)線程的線程池其實(shí)就是指定線程數(shù)為1的固定線程池,主要區(qū)別就是,返回的Executor實(shí)例用了一個(gè)FinalizableDelegatedExecutorService
對(duì)象進(jìn)行包裝。
我們來看下FinalizableDelegatedExecutorService,該類 只定義了一個(gè)finalize方法:
static class FinalizableDelegatedExecutorService extends DelegatedExecutorService { FinalizableDelegatedExecutorService(ExecutorService executor) { super(executor); } protected void finalize() { super.shutdown(); } }
核心是其繼承的DelegatedExecutorService ,這是一個(gè)包裝類,實(shí)現(xiàn)了ExecutorService的所有方法,但是內(nèi)部實(shí)現(xiàn)其實(shí)都委托給了傳入的ExecutorService 實(shí)例:
/** * ExecutorService實(shí)現(xiàn)類的包裝類. */ /** * A wrapper class that exposes only the ExecutorService methods * of an ExecutorService implementation. */ static class DelegatedExecutorService extends AbstractExecutorService { private final ExecutorService e; DelegatedExecutorService(ExecutorService executor) { e = executor; } public void execute(Runnable command) { e.execute(command); } public void shutdown() { e.shutdown(); } public List<Runnable> shutdownNow() { return e.shutdownNow(); } public boolean isShutdown() { return e.isShutdown(); } public boolean isTerminated() { return e.isTerminated(); } public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { return e.awaitTermination(timeout, unit); } public Future<?> submit(Runnable task) { return e.submit(task); } public <T> Future<T> submit(Callable<T> task) { return e.submit(task); } public <T> Future<T> submit(Runnable task, T result) { return e.submit(task, result); } public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException { return e.invokeAll(tasks); } public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException { return e.invokeAll(tasks, timeout, unit); } public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException { return e.invokeAny(tasks); } public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return e.invokeAny(tasks, timeout, unit); } }
為什么要多此一舉,加上這樣一個(gè)委托層?因?yàn)榉祷氐腡hreadPoolExecutor包含一些設(shè)置線程池大小的方法——比如setCorePoolSize,對(duì)于只有單個(gè)線程的線程池來說,我們是不希望用戶通過強(qiáng)轉(zhuǎn)的方式使用這些方法的,所以需要一個(gè)包裝類,只暴露ExecutorService本身的方法。
有些情況下,我們雖然創(chuàng)建了具有一定線程數(shù)的線程池,但出于資源利用率的考慮,可能希望在特定的時(shí)候?qū)€程進(jìn)行回收(比如線程超過指定時(shí)間沒有被使用),Executors就提供了這種類型的線程池:
/** * 創(chuàng)建一個(gè)可緩存線程的Execotor. * 如果線程池中沒有線程可用, 則創(chuàng)建一個(gè)新線程并添加到池中; * 如果有線程長時(shí)間未被使用(默認(rèn)60s, 可通過threadFactory配置), 則從緩存中移除. */ public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } /** * 創(chuàng)建一個(gè)可緩存線程的Execotor. * 如果線程池中沒有線程可用, 則創(chuàng)建一個(gè)新線程并添加到池中; * 如果有線程長時(shí)間未被使用(默認(rèn)60s, 可通過threadFactory配置), 則從緩存中移除. * 在需要時(shí)使用提供的 ThreadFactory 創(chuàng)建新線程. */ public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory); }
可以看到,返回的還是ThreadPoolExecutor對(duì)象,只是指定了超時(shí)時(shí)間,另外線程池中線程的數(shù)量在[0, Integer.MAX_VALUE]
之間。
如果有任務(wù)需要延遲/周期調(diào)用,就需要返回ScheduledExecutorService接口的實(shí)例,ScheduledThreadPoolExecutor
就是實(shí)現(xiàn)了ScheduledExecutorService接口的一種Executor,和ThreadPoolExecutor一樣,這個(gè)我們后面會(huì)專門講解。
/** * 創(chuàng)建一個(gè)具有固定線程數(shù)的 可調(diào)度Executor. * 它可安排任務(wù)在指定延遲后或周期性地執(zhí)行. */ public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); } /** * 創(chuàng)建一個(gè)具有固定線程數(shù)的 可調(diào)度Executor. * 它可安排任務(wù)在指定延遲后或周期性地執(zhí)行. * 在需要時(shí)使用提供的 ThreadFactory 創(chuàng)建新線程. */ public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) { return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); }
Fork/Join線程池是比較特殊的一類線程池,在JDK1.7時(shí)才引入,其核心實(shí)現(xiàn)就是ForkJoinPool
類。關(guān)于Fork/Join框架,我們后面會(huì)專題講解,現(xiàn)在只需要知道,Executors框架提供了一種創(chuàng)建該類線程池的便捷方法。
/** * 創(chuàng)建具有指定并行級(jí)別的ForkJoin線程池. */ public static ExecutorService newWorkStealingPool(int parallelism) { return new ForkJoinPool(parallelism, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); } /** * 創(chuàng)建并行級(jí)別等于CPU核心數(shù)的ForkJoin線程池. */ public static ExecutorService newWorkStealingPool() { return new ForkJoinPool(Runtime.getRuntime().availableProcessors(), ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true); }
至此,Executors框架的整體結(jié)構(gòu)基本就講解完了,此時(shí)我們的腦海中應(yīng)有大致如下的一幅類繼承圖:
下面來回顧一下,上面的各個(gè)接口/類的關(guān)系和作用:
Executor
執(zhí)行器接口,也是最頂層的抽象核心接口, 分離了任務(wù)和任務(wù)的執(zhí)行。ExecutorService
在Executor的基礎(chǔ)上提供了執(zhí)行器生命周期管理,任務(wù)異步執(zhí)行等功能。ScheduledExecutorService
在ExecutorService基礎(chǔ)上提供了任務(wù)的延遲執(zhí)行/周期執(zhí)行的功能。Executors
生產(chǎn)具體的執(zhí)行器的靜態(tài)工廠ThreadFactory
線程工廠,用于創(chuàng)建單個(gè)線程,減少手工創(chuàng)建線程的繁瑣工作,同時(shí)能夠復(fù)用工廠的特性。AbstractExecutorService
ExecutorService的抽象實(shí)現(xiàn),為各類執(zhí)行器類的實(shí)現(xiàn)提供基礎(chǔ)。ThreadPoolExecutor
線程池Executor,也是最常用的Executor,可以以線程池的方式管理線程。ScheduledThreadPoolExecutor
在ThreadPoolExecutor基礎(chǔ)上,增加了對(duì)周期任務(wù)調(diào)度的支持。ForkJoinPool
Fork/Join線程池,在JDK1.7時(shí)引入,時(shí)實(shí)現(xiàn)Fork/Join框架的核心類。
上述內(nèi)容就是Java J.U.C中executors框架的設(shè)計(jì)理念是什么,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。