| corePoolSize:核心线程池大小 cSize |
| maximumPoolSize:线程池最大容量 mSize |
| keepAliveTime:当线程数量大于核心时,多余的空闲线程在终止之前等待新任务的最大时间 |
| unit:时间单位 |
| workQueue: 工作队列 nWorks |
| ThreadFactory:线程工厂 |
| handler:拒绝策略 |
| 运行机制如下: |
| 通过new创建线程池时,除非调用prestartAllCoreThreads方法初始化核心线程,否则此时线程池中有0个线程,即使工作队列中存在多个任务,同样 |
| 不会执行 |
| public class ThreadPoolDemo { |
| |
| public static void main(String[] args) throws ExecutionException, InterruptedException { |
| |
| LinkedBlockingQueue<Runnable> objects = new LinkedBlockingQueue<>(); |
| |
| for (int i = 0; i <100 ; i++) { |
| objects.put(()->{ |
| System.out.println(Thread.currentThread().getName()); |
| }); |
| } |
| |
| ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 3L, |
| TimeUnit.SECONDS, objects); |
| } |
| |
| } |
| |
| # 控制台:不会执行 |
| public class ThreadPoolDemo { |
| |
| public static void main(String[] args) throws ExecutionException, InterruptedException { |
| |
| LinkedBlockingQueue<Runnable> objects = new LinkedBlockingQueue<>(); |
| |
| for (int i = 0; i <100 ; i++) { |
| objects.put(()->{ |
| System.out.println(Thread.currentThread().getName()); |
| }); |
| } |
| |
| ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 3L, |
| TimeUnit.SECONDS, objects); |
| |
| threadPoolExecutor.prestartAllCoreThreads(); |
| } |
| |
| } |
| |
| # 控制台打印成功 |
| 任务数X |
| x <= cSize 只启动x个线程 |
| x >= cSize && x < nWorks + cSize 会启动 <= cSize 个线程 其他的任务就放到工作队列里 |
| x > cSize && x > nWorks + cSize |
| x-(nWorks) <= mSize 会启动x-(nWorks)个线程 |
| x-(nWorks) > mSize 会启动mSize个线程来执行任务,其余的执行相应的拒绝策略 |
| public class ThreadPoolDemo { |
| |
| public static void main(String[] args) throws ExecutionException, InterruptedException { |
| |
| LinkedBlockingQueue<Runnable> objects = new LinkedBlockingQueue<>(); |
| |
| ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 3L, |
| TimeUnit.SECONDS, objects); |
| |
| threadPoolExecutor.prestartAllCoreThreads(); |
| |
| for (int i = 0; i < 5; i++) { |
| threadPoolExecutor.submit(()->{ |
| System.out.println(threadPoolExecutor.getActiveCount()); |
| }); |
| } |
| } |
| |
| } |
| |
| # 控制台打印结果:有5个活跃的任务 |
| 5 |
| 5 |
| 5 |
| 5 |
| 5 |
- 案例4:启动的任务数量大于核心线程数,小于工作队列数+核心线程数
| public class ThreadPoolDemo { |
| |
| public static void main(String[] args) throws ExecutionException, InterruptedException { |
| |
| LinkedBlockingQueue<Runnable> objects = new LinkedBlockingQueue<>(20); |
| |
| ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 3L, |
| TimeUnit.SECONDS, objects); |
| |
| threadPoolExecutor.prestartAllCoreThreads(); |
| |
| for (int i = 0; i < 20; i++) { |
| threadPoolExecutor.submit(()->{ |
| try { |
| Thread.sleep(2000L); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| System.out.println(threadPoolExecutor.getActiveCount()); |
| }); |
| } |
| } |
| |
| } |
| |
| # 控制台打印如下: |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 10 |
| 9 |
| 9 |
| 9 |
| 9 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-05-16 真实机中安装CentOS7(一)