线程池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相关的线程池。

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