Java 線程池 從入門(mén)到深入

類(lèi)圖

該類(lèi)圖也是從左到右按入門(mén)到深入繪制的。

Java 線程池.png

入門(mén)

創(chuàng)建線程池實(shí)例

使用java.util.concurrent.Executors工具類(lèi)創(chuàng)建線程池。
Executors工具類(lèi)提供如下靜態(tài)方法來(lái)創(chuàng)建多種類(lèi)型的線程池實(shí)例:

public static ExecutorService newSingleThreadExecutor()
public static ExecutorService newFixedThreadPool(int nThreads) 
public static ExecutorService newCachedThreadPool() 
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
public static ScheduledExecutorService newSingleThreadScheduledExecutor()
public static ExecutorService newWorkStealingPool()

Executors抽象的常用的線程池類(lèi)型

  • 單線程線程池
public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }
  • 固定工作線程數(shù)量大小線程池
public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
  • 可緩存線程池
public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
  • 定時(shí)周期性線程池
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
  • 單線程定時(shí)周期性線程池
public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }
  • 竊取工作的線程池
public static ExecutorService newWorkStealingPool() {
        return new ForkJoinPool
            (Runtime.getRuntime().availableProcessors(),
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }

通過(guò)線程池運(yùn)行線程

可以提交單個(gè)、多個(gè)線程。

提交單個(gè)線程

  • java.util.concurrent.Executor#execute 無(wú)返回
  • java.util.concurrent.ExecutorService#submit(java.util.concurrent.Callable<T>) 返回Future

提交多個(gè)線程

  • java.util.concurrent.ExecutorService#invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>>)
  • java.util.concurrent.ExecutorService#invokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>>)

停止線程池

  • void shutdown() 啟動(dòng)有序關(guān)閉,在此過(guò)程中執(zhí)行先前提交的任務(wù),但不接受新的任務(wù)。之后提交的任務(wù)將拋出java.util.concurrent.RejectedExecutionException異常。
  • List<Runnable> shutdownNow(); 嘗試停止所有正在執(zhí)行的任務(wù),停止對(duì)正在等待的任務(wù)的處理,并返回正在等待執(zhí)行的任務(wù)的列表。、

線程池結(jié)束狀態(tài)

  • boolean isShutdown(); 如果這個(gè)執(zhí)行器已經(jīng)被關(guān)閉,返回true
  • boolean isTerminated(); 如果關(guān)閉后所有任務(wù)都已完成,則返回true。注意,除非首先調(diào)用shutdown或shutdownNow,否則isTerminated永遠(yuǎn)不會(huì)為真。

Executors創(chuàng)建線程池不足

是否真的存在阿里巴巴Java開(kāi)發(fā)手冊(cè)中存在的問(wèn)題有待考量。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容