線程池

ArrayBlockingQueue 有界阻塞隊列,此隊列會滿。
SynchronousQueue 沒有容量的阻塞隊列,一次添加必須等待一次獲取。反之亦然。
LinkedBlockingQueue 無界阻塞隊列 這個阻塞隊列用于不會滿

線程池

固定線程數(shù)的線程池
1.核心 線程數(shù) 和 最大線程數(shù)一致。
2.采用的阻塞隊列是 無界阻塞隊列,LinkedBlockingQueue。也就是在極端情況下,阻塞隊列 會一直增長,直到堆內存溢出,需要謹慎使用該線程池。

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>(),
                                  threadFactory);
}

緩沖線程池
0).在newCachedThreadPool中如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。

1).初看該構造函數(shù)時我有這樣的疑惑:核心線程池為0,那按照前面所講的線程池策略新任務來臨時無法進入核心線程池,只能進入 SynchronousQueue中進行等待
,而SynchronousQueue的大小為1,那豈不是第一個任務到達時只能等待在隊列中,直到第二個任務到達發(fā)現(xiàn)無法進入隊列才能創(chuàng)建第一個線程?

2).這個問題的答案在上面講SynchronousQueue時其實已經(jīng)給出了,要將一個元素放入SynchronousQueue中,必須有另一個線程正在等待接收這個元素。因此即便SynchronousQueue一開始為空且大小為1,第一個任務也無法放入其中,因為沒有線程在等待從SynchronousQueue中取走元素。因此第一個任務到達時便會創(chuàng)建一個新線程執(zhí)行該任務。

3).這個最大線程數(shù)是 Integer.MAX 使用中需要注意。

public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>(),
                                  threadFactory);
}

newWorkStealingPool
創(chuàng)建一個帶并行級別的線程池,并行級別決定了同一時刻最多有多少個線程在執(zhí)行,如不穿如并行級別參數(shù),將默認為當前系統(tǒng)的CPU個數(shù)

public static ExecutorService newWorkStealingPool(int parallelism) {
//第四個參數(shù)
// asyncMode ? FIFO_QUEUE : LIFO_QUEUE,
    return new ForkJoinPool
        (parallelism,
         ForkJoinPool.defaultForkJoinWorkerThreadFactory,
         null, true);
}

public static ExecutorService newWorkStealingPool() {
    return new ForkJoinPool
        (Runtime.getRuntime().availableProcessors(),
         ForkJoinPool.defaultForkJoinWorkerThreadFactory,
         null, true);
}
public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>(),
                                threadFactory));
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1));
}

public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1, threadFactory));
}

創(chuàng)建一個定長線程池,支持定時及周期性任務執(zhí)行。

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}

public static ScheduledExecutorService newScheduledThreadPool(
        int corePoolSize, ThreadFactory threadFactory) {
    return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 線程的兩種創(chuàng)建方式:繼承Thread類或者實現(xiàn)Runnable接口,Thread類本質上是實現(xiàn)了Runnable接...
    繁星追逐閱讀 712評論 0 1
  • 第6章介紹了任務執(zhí)行框架, 它不僅能簡化任務與線程的生命周期管理, 而且還提供一 種簡單靈活的方式將任務的提交與任...
    好好學習Sun閱讀 1,311評論 0 2
  • Java線程池 [toc] 什么是線程池 線程池就是有N個子線程共同在運行的線程組合。 舉個容易理解的例子:有個線...
    石家志遠閱讀 1,438評論 0 6
  • 線程池主要用來解決線程生命周期開銷問題和資源不足問題。通過對多個任務重復使用線程,線程創(chuàng)建的開銷就被分攤到了多個任...
    安仔夏天勤奮閱讀 1,127評論 0 10
  • G:【自我感悟】每次分享或講課,不到最后一刻總是交不出課件,原來一直以為這是我的拖延癥所致。然而并不是,通過跑步的...
    無憂俠閱讀 160評論 0 0

友情鏈接更多精彩內容