線程池5th卷:不畏浮雲遮望眼

隨着業務發展的需要,總公司集團老總(Doug Lea ) 提出了能支持預定功能(scheduled)的經營模式(ScheduledExecutorService)。PS: schedule:安排,計劃,預定


1.戰略:ScheduledExecutorService

觀摩一下其內部新增的功能

  1. schedule(Runnable/Callale, delay, TimeUnit ):在指定延遲之後執行一次(one-shot)任務
  2. scheduleAtFixedRate(Runnable , initialDelay, period,TimeUnit ):在initDelay後,以period的固定間隔執行任務
  3. scheduleWithFixedDelay(Runnable , initialDelay, delay, TimeUnit ):在initDelay後,以固定的delay延時執行任務

根據上面的描述,我們可以發現ScheduledExecutorService主要推出的功能是:延時,定時,週期性服務、

2.戰術:ScheduledThreadPoolExecutor

正所謂“萬變不離其宗”,新模式(**ScheduledExecutorService)**也是基於舊模式(ThreadPoolExecutor)而來的。都說“青出於藍而勝於藍”,不過眼見爲實…

public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue());
    }
  public ScheduledThreadPoolExecutor(int corePoolSize,
                                       ThreadFactory threadFactory) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue(), threadFactory);
    }
 public ScheduledThreadPoolExecutor(int corePoolSize,
                                       RejectedExecutionHandler handler) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue(), handler);
    }
 public ScheduledThreadPoolExecutor(int corePoolSize,
                                       ThreadFactory threadFactory,
                                       RejectedExecutionHandler handler) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue(), threadFactory, handler);
    }

通過上面的構造函數(經營模板),我們會發現,總公司應該是結合以往的經驗,對新模式進行了比較大的優化。

  1. 必須指定正式工(corePoolSize);
  2. 臨時工的數量無限制(maxiumPoolSize = Integer.MAX_VALUE),結合成本只能keepAliveTime = 0.
  3. 排隊方式(使用DelayedWorkQueue)進行優化. (PS:初始容量爲16,基於數據結構堆(heap-based)而設計,加速離隊速度(O(n) -> O(log n)),減少城管的騷擾(減少垃圾回收)
  4. 開放拒絕策略(RejectedExecutionHandler)
  5. 支持選擇廚師培訓學校(ThreadFactory)

3. 實施:Executors

如果普通客戶來連鎖店,一般都會來找Executors…只此一家,別無分店.

  /** Cannot instantiate. 不能實例化
	*/
    private Executors() {}

着重強調一下:Executors一般都只會開放corePoolSizeThreadFactory兩個選項。

爲客戶着想使我們應該做的,

單廚師套餐

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

多廚師套餐

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

後面我們會專門出一篇來講總代理(Executors)以及VIP客戶定製化,圓你一個開店做老闆的夢…

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