juc线程池原理(六):jdk线程池中的设计模式

一、jdk中默认线程池中的代理模式

单例类线程池只有一个线程,无边界队列,适合cpu密集的运算。jdk中创建线程池是通过Executors类中提供的静态的方法来创建的,其中的单例类线程池的方法如下:

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

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

说明:

1、newSingleThreadExecutor的代理模式类图如下:

new ThreadPoolExecutor()实例是被代理对象作为参数传给代理对象FinalizableDelegatedExecutorService,通过代理对象限制对ThreadPoolExecutor进行参数化调整。
2、newSingleThreadScheduledExecutor的代理模式类图如下:



new ScheduledThreadPoolExecutor(1)实例是被代理对象作为参数传递给代理对象DelegatedScheduledExecutorService,通过代理对象限制对ThreadPoolExecutor进行参数化调整。

原文地址:https://www.cnblogs.com/duanxz/p/3400051.html