1. 程式人生 > >執行緒池原理--工廠類Executors

執行緒池原理--工廠類Executors

文章目錄


執行緒池原理–總索引

執行緒池原理–工廠類Executors

Executors類是一個工廠類,用於建立ScheduledExecutorService物件。
不過《阿里巴巴Java開發手冊》不建議使用Executors類,而是直接使用ScheduledThreadPoolExecutor來建立執行緒池物件。

4. 【強制】執行緒池不允許使用 Executors 去建立,而是通過 ThreadPoolExecutor 的方式,
這樣的處理方式讓寫的同學更加明確執行緒池的執行規則,規避資源耗盡的風險。
說明: Executors 返回的執行緒池物件的弊端如下:
1) FixedThreadPool 和 SingleThreadPool:
允許的請求佇列長度為 Integer.MAX_VALUE,可能會堆積大量的請求,從而導致 OOM。
2) CachedThreadPool 和 ScheduledThreadPool:
允許的建立執行緒數量為 Integer.MAX_VALUE, 可能會建立大量的執行緒,從而導致 OOM。

構造器

由於是工廠類,相關的方法都是static型別,因此將構造器隱藏了。

 private Executors() {}

newFixedThreadPool

建立一個固定大小的執行緒池:通過重用共享無界佇列裡的執行緒來減少執行緒建立的開銷。當所有的執行緒都在執行任務,新增的任務將會在佇列中等待,直到一個執行緒空閒。由於在執行前失敗導致的執行緒中斷,如果需要繼續執行接下去的任務,新的執行緒會取代它執行。執行緒池中的執行緒會一直存在,除非明確地 shutdown 掉。

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); }

newSingleThreadExecutor

建立一個單個執行緒的執行緒池。任務會被保證順序執行,因為只有一個工作執行緒。不像 newFixedThreadPool(1),這個不保證任務順序執行。corePoolSize 和 maximumPoolSize 都是 1。

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));
    }

newCachedThreadPool

建立一個可按需自動擴容的執行緒池,但是會優先重用執行緒池中空閒可用的執行緒。這個型別的執行緒池將會大大提升執行許多短暫的非同步任務的程式。如果執行緒池中執行緒都在使用,又有新任務到來,則新增一個執行緒到執行緒池。如果執行緒 60 秒內空閒,則將被終止移除執行緒池。corePoolSize 為 0,可知一旦執行緒 60s 空閒就會被移出執行緒池

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);
    }

newScheduledThreadPool

建立一個在一定延遲時間後排程命令的執行緒池,或者週期性執行的執行緒池。

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

newSingleThreadScheduledExecutor

建立一個在一定延遲時間後排程命令的執行緒池,或者週期性執行的執行緒池。大小為1.

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