線程池4th卷:大鵬展翅恨天低

線程池(飯店)的各個環節你都摸清楚了,而且在餐飲業已經積累了口碑,資源,技術,,但你不會忘記你當初的目標:融資上市…。夢想雖遠,好在路在腳下。開加盟連鎖店,爲志同道合的創業人士開闢一條捷徑

縱觀Java多線程的設計,我們目前所討論的只是其中的一部分。
Java多線程的總佈局
這裏主要討論:ThreadPoolExecutor,後續將全面展開,若有期待,請下方評論@。

1. 初級階段:newSingleThreadExecutor

單個線程的線程池

創業初期,哪來那麼多錢啊。招一個廚師(Thread)先看看發展情況。

 public static ExecutorService newSingleThreadExecutor() {
 		// 注意 FinalizableDelegatedExecutorService,後面會專門來講它。
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

可以指定:廚師培訓學校(ThreadFactory)

 public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>(),
                                    threadFactory));
    }

2. 中級:newFixedThreadPool

固定線程數量的線程池

發現生意有搞頭,擴大規模,多招幾個廚師(Thread)

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

知道廚師(Thread)不好管理,所以可以指定:廚師培訓學校(ThreadFactory)

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

3. 階段性總結

newSingleThreadExecutornewFixedThreadPool,兩者都屬於簡單粗暴型,沒有經營頭腦。這裏我們不妨看一下:

  1. 廚師(線程)數量固定(corePoolSize = MaxiumPoolSize);
  2. 不考慮排隊的影響(LinkedBlockingQueue),
  3. 拒絕策略:顧客敢BB就翻臉(AbortPolicy:直接拒絕且拋出異常)

PS: 兩者都不會隨便辭退廚師(業界良心典範),雖然KeepAliveTime=0,但是由於corePoolSize = maxiumPoolSize 且allowCoreThreadTimeOut默認爲false, 所以就算店裏沒生意,廚師空閒也只能等着生意上門。


爲上帝服務,就是爲顧客服務

作爲總公司(Executors)的董事長,鑑於newSingleThreadExecutornewFixedThreadPool的表演,你在集團會議上確立了:爲上帝服務,就是爲顧客服務的服務宗旨。着重強調如果你想讓顧客們近悅遠來,就該努力提升服務品質的會議精神。

4. 改進型:newCachedThreadPool

爲了提升服務質量,推出了無需等位,快速響應服務。

無需等位:SynchronousQueue 同步隊列
快速響應:(maxiumPoolSize = Integer.MAX_VALUE)且corePoolSize = 0, keepAliveTime = 60L

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

隨着外賣業務的興起,我們對飯店提出了更高的要求**(延時處理,定時處理,輪詢處理**),我們將在下一篇文章來展開Scheduled相關的線程池。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章