一、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的代理模式类图如下:

JDK线程池源码之七:jdk线程池中的设计模式

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

JDK线程池源码之七:jdk线程池中的设计模式

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

策略模式

线程池中的拒绝

相关文章:

  • 2022-12-23
  • 2021-12-20
  • 2021-06-08
  • 2022-12-23
  • 2021-06-29
  • 2021-08-18
  • 2021-12-30
  • 2023-04-04
猜你喜欢
  • 2022-01-19
  • 2021-06-28
  • 2022-12-23
  • 2022-02-09
  • 2021-12-01
  • 2021-04-14
  • 2022-02-08
相关资源
相似解决方案