05-JUC-ThreadPoolExecutor应用
介绍
ExecutorPoolExecutor有几种类型:
newFixedThreadPool newSingleThreadExecutor newCachedThreadPool scheduledThreadExecutor
见名知意,newFixedThreadPool 是固定线程数的线程池,newSingleThreadExecutor 是只有一个线程的线程池,
newCachedThreadPool,看到cache是指缓存的意思,也就是里面的线程数是不固定的线程池
scheduledThreadExecutor是定时的执行任务的线程池,类似于Timer一样。
看下 ThreadPoolExecutor 的构造函数
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); }
可以简单推测下,
- newFixedThreadPool实际上就是 corePoolSize 和maximumPoolSize都固定的参数的线程池
- newSingleThreadExecutor其实就是corePoolSize为1,且maximumPoolSize也为1的线程池
- newCachedThreadPool,核心线程数 最大线程数可能是一个视情况而定的一个范围
- scheduledThreadExecutor 带有延时效果的,并且设置了核心线程数的线程池
下面来详细说说上面提到的线程池
newSingleThreadExecutor
newSingleThreadExecutor在Executors类里面,看看是怎么定义的:
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
哈哈哈哈,就是这样定义的
- corePoolSize核心线程数为1,maximumPoolSize最大线程数为1
- 使用了一个无界队列保存待执行的任务
- 任何时候都只有一个线程在执行任务,并且哪些待执行的任务有序的等待被执行
newFixedThreadExecutor
看看newFixedThreadExecutor在Executors里面的定义
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
- 核心线程数和最大线程数是固定的,由你自己定义
- 使用了一个无界队列保存待执行的任务
- 任何时候最多有nThreads个线程在执行,如果有nThreads个线程正处于活跃状态执行任务,如果有新的任务提交过来,新的任务会放到阻塞队列Queue中等待
newCachedThreadPool
看看newCachedThreadPool在Executors里面的定义
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
- 核心线程数为0,最大线程数为integer的最大值。空闲时间为60秒,单位是秒
- 会视情况而定创建线程,内部使用的SynchronousQueue阻塞队列
- 空闲的线程如果空闲时间超过60s将会被终止并且从线程池中移除掉
ScheduledThreadPoolExecutor
看下ScheduledThreadPoolExecutor的定义
public ScheduledThreadPoolExecutor(int corePoolSize) { super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue()); }
- 核心线程数自定义,最大线程数为integer的最大值
- 利用了一个延时的工作队列保存待执行的任务
栗子
以newFixedThreadPool为例子
public class ThreadWorker implements Runnable { private String exeCommand; public ThreadWorker(String exeCommand) { this.exeCommand = exeCommand; } @Override public void run() { System.out.println(Thread.currentThread().getName() + " 开始执行:" + exeCommand); //模拟任务执行 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " 结束执行。。"); } } public class NewFixedThreadPoolDemo { public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(3); for (int i = 0; i < 8; i++) { Runnable runnable = new ThreadWorker("" + i); executorService.execute(runnable); } executorService.shutdown(); while(!executorService.isTerminated()){ } System.out.println("运行完所有的线程"); } }
运行结果:
pool-1-thread-1 开始执行:0 pool-1-thread-2 开始执行:1 pool-1-thread-3 开始执行:2 pool-1-thread-3 结束执行。。 pool-1-thread-2 结束执行。。 pool-1-thread-1 结束执行。。 pool-1-thread-2 开始执行:3 pool-1-thread-3 开始执行:4 pool-1-thread-1 开始执行:5 pool-1-thread-3 结束执行。。 pool-1-thread-2 结束执行。。 pool-1-thread-2 开始执行:7 pool-1-thread-1 结束执行。。 pool-1-thread-3 开始执行:6 pool-1-thread-2 结束执行。。 pool-1-thread-3 结束执行。。 运行完所有的线程
本文作者:hu_volsnow
本文链接:https://www.cnblogs.com/volsnow/p/15791179.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步