原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11443644.html

 

newFixedThreadPool

重用指定数目(nThreads)的线程,其背后使用的是无界的工作队列,任何时候最多有nThreads个工作线程是活动的。这意味着,如果任务数量超过了活动队列数目,将在工作队列中等待空闲线程出现;如果有工作线程退出,将会有新的工作线程被创建,以补足指定的数目nThreads。

ExecutorService newFixedThreadPool(int nThreads)

JUC线程池
ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)

JUC线程池

 

newCachedThreadPool

它是一种用来处理大量短时间工作任务的线程池,具有几个鲜明特点:它会试图缓存线程并重用,当无缓存线程可用时,就会创建新的工作线程;如果线程闲置的时间超过60秒,则被终止并移出缓存;长时间闲置时,这种线程池,不会消耗什么资源。其内部使用SynchronousQueue作为工作队列。

ExecutorService newCachedThreadPool()

JUC线程池

ExecutorService newCachedThreadPool(ThreadFactory threadFactory)

JUC线程池

 

newSingleThreadExecutor 

它的特点在于工作线程数目被限制为1,操作一个无界的工作队列,所以它保证了所有任务的都是被顺序执行,最多会有一个任务处于活动状态,并且不允许使用者改动线程池实例,因此可以避免其改变线程数目。

ExecutorService newSingleThreadExecutor()

JUC线程池

ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)

JUC线程池

 

ScheduledThreadPoolExecutor

创建的是个ScheduledExecutorService,可以进行定时或周期性的工作调度,区别在于单一工作线程还是多个工作线程。

ScheduledExecutorService newScheduledThreadPool(int corePoolSize)

JUC线程池
ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory)

JUC线程池
ScheduledExecutorService newSingleThreadScheduledExecutor()

JUC线程池
ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory)

JUC线程池

 

newWorkStealingPool

Java 8才加入这个创建方法,其内部会构建ForkJoinPool,利用Work-Stealing算法,并行地处理任务,不保证处理顺序。

ExecutorService newWorkStealingPool(int parallelism)

JUC线程池
ExecutorService newWorkStealingPool()

JUC线程池

 

相关文章:

  • 2021-12-18
  • 2020-05-24
  • 2022-01-17
  • 2020-05-28
  • 2019-10-11
  • 2022-01-07
  • 2021-12-29
猜你喜欢
  • 2021-10-16
  • 2022-01-16
  • 2021-04-26
  • 2021-09-27
  • 2021-10-08
  • 2022-01-07
  • 2021-11-13
  • 2021-09-20
相关资源
相似解决方案