一分钟做网站,网站构思,营销方案怎么写模板,中国金湖建设网站#x1f3f7;️个人主页#xff1a;牵着猫散步的鼠鼠 #x1f3f7;️系列专栏#xff1a;Java全栈-专栏 #x1f3f7;️本系列源码仓库#xff1a;多线程并发编程学习的多个代码片段(github) #x1f3f7;️个人学习笔记#xff0c;若有缺误#xff0c;欢迎评论区指正… ️个人主页牵着猫散步的鼠鼠 ️系列专栏Java全栈-专栏 ️本系列源码仓库多线程并发编程学习的多个代码片段(github) ️个人学习笔记若有缺误欢迎评论区指正 ✨️本系列源码均已上传仓库 1321928757/Concurrent-MulThread-Demo(github.com)✨️ (本章节可参考liushijie-240409-lifecycle分支) 1.前言
在前面几篇文章中,我们已经实现了线程池的核心功能:任务队列、执行逻辑以及线程管理。本次我们将继续扩展补充线程池的功能为线程池添加生命周期管理。 往期文章传送门Java多线程实战-从零手搓一个简易线程池(一)定义任务等待队列-CSDN博客 Java多线程实战-从零手搓一个简易线程池(二)线程池与拒绝策略实现-CSDN博客 Java多线程实战-从零手搓一个简易线程池(三)线程工厂核心线程与非核心线程逻辑实现-CSDN博客 2.为什么要加入生命周期
通过引入生命周期我们能够更加灵活地管理和控制线程的创建、运行和销毁过程。可以更好地处理资源分配、任务调度和系统稳定性等方面的问题。
2.1.生命周期的主要作用包括 资源管理线程池中的线程是一种昂贵的资源通过引入生命周期可以确保线程在适当的时机被创建和销毁避免资源的浪费。 系统稳定性线程池生命周期的管理可以帮助我们避免因线程过多或过少而导致的系统不稳定问题。通过合理地控制线程的数量可以确保系统在处理高并发任务时仍能保持稳定运行。 任务调度线程池生命周期的管理可以帮助我们更好地进行任务调度确保任务能够按照预期的方式执行。例如我们可以通过设置线程池的最大线程数和任务队列长度等参数来控制任务的执行顺序和并发度。 系统扩展性通过引入生命周期我们可以为线程池添加更多的功能和特性如线程池监控、任务统计等。这些功能和特性可以帮助我们更好地了解线程池的运行状况从而对其进行优化和扩展。
2.2.线程池的状态分类
线程池的状态一共有五种分别是RUNNING、SHUTDOWN、STOP、TIDYING、TERMINATED
RUNNING表示可接受新任务且可执行队列中的任务SHUTDOWN表示不接受新任务但可执行队列中的任务STOP表示不接受新任务且不再执行队列中的任务且中断正在执行的任务TIDYING所有任务已经中止且工作线程数量为0最后变迁到这个状态的线程将要执行terminated()钩子方法只会有一个线程执行这个方法TERMINATED中止状态已经执行完terminated()钩子方法
为了简化过程我们这里就简单实现三种状态RUNNING, SHUTDOWN, STOP
3.设计思路
对于线程池的状态管理我们这里实现两个最常用的方法shutdown和shutdownNow这两个方法都会关闭线程池前者为优雅关闭会等待全部任务执行完成后关闭线程池而后者是立刻关闭他会中断所有正在运行的线程不管任务有没有执行完成。
我们简易线程池的状态流转如下
当线程池初始化创建时默认为Running状态我们调用shutdown
4.代码实现
4.1.定义状态相关字段
我们在ThreadPool对象中定义AtomicInteger 原子类作为线程池状态 /** 线程池状态常量*/private static final int RUNNING 1;private static final int SHUTDOWN 2;private static final int STOP 3;private static final int TIDYING 4;private static final int TERMINATED 5;/** 线程池当前状态*/private final AtomicInteger state new AtomicInteger(RUNNING);
4.2.修改excute方法
我们添加了一个状态判断如果线程池状态为SHUTDOWN以上我们直接拒绝任务 public boolean isShutdown() {return state.get() SHUTDOWN;}
public void execute(Runnable task){if(task null){throw new NullPointerException(传递的Runnable任务为Null);}// 1.如果线程池状态为SHUTDOWN以上不再接受任务直接触发拒绝策略if(isShutdown()){reject(task);}// 2.如果当前线程数小于核心线程,直接创建线程去运行if(threadTotalNums.get() corePoolSize){if(addWorker(task, true)) return;}// 3.线程数大于核心线程我们就将任务加入等待队列if(workQueue.offer(task)){return;}// 4.队列满了尝试创建非核心线程如果失败就触发拒绝策略else if(!addWorker(task, false)){reject(task);}}
4.2.修改addWorker方法
我们在addWorker方法添加工作线程前加入线程池状态的判断当线程池状态不为Running(大于Running)时我们直接返回false不再添加工作线程 c SHUTDOWN !workQueue.isEmpty() 这个条件是指当线程池为SHUTDOWN状态但workQueue不为空此时我们可能还需要创建新的线程来加速处理速度所以这种情况下我们不应该返回false public Boolean addWorker(Runnable firstTask, Boolean isCore){if(firstTask null) {throw new NullPointerException();}// 1.生命周期检查不在RUNNING状态下不继续添加工作线程但是可能存在刚关闭线程池的情况此时状态为shutdown如果任务队列不为空我们依旧允许创建线程来加快任务处理final int c state.get();if (c RUNNING !(c SHUTDOWN !workQueue.isEmpty())) {return false;}// 2.根据当前线程池和isCore条件判断是否需要创建int wc threadTotalNums.get();if (wc (isCore ? corePoolSize : maximumPoolSize))return false;// 3.创建线程并添加到线程集合中Worker worker new Worker(firstTask);Thread t worker.thread;if(t ! null){synchronized (workerSet){workerSet.add(worker);threadTotalNums.getAndIncrement();}t.start();return true;}return false;}
4.3.修改getTask方法
当线程池状态为SHUTDOWN 以上时并且满足状态大于Stop或者任务队列为空两者条件之一时我们停止获取任务直接返回null shutdown状态下只是不接受新任务了但是队列中原有的任务还是会执行而stop状态下是队列中的任务也不执行了 4.4定义shutdownshutdownNow等状态管理方法 shutdown方法我们直接调用原子类的CAS操作来切换状态 public void shutdown() {// 如果为if (state.compareAndSet(RUNNING, SHUTDOWN)) {log.info(线程池正在关闭);tryTerminate(); // 尝试转换到TERMINATED状态}}
shotdownNow方法我们遍历线程集合中断所有运行中的线程这里需要注意异常处理
public void shutdownNow() {if (state.compareAndSet(RUNNING, STOP)) {try {log.info(线程池立即关闭尝试中断所有线程);// 中断所有正在运行的线程synchronized (workerSet){for (Worker worker : workerSet) {worker.thread.interrupt();}}} finally {state.set(TIDYING);// 在此处执行清理工作transitionToTerminated();}}} tryTerminate与transitionToTerminated方法 private void tryTerminate() {if ((state.get() SHUTDOWN || state.get() STOP) workQueue.isEmpty() workerSet.isEmpty()) {if (state.compareAndSet(SHUTDOWN, TIDYING) || state.compareAndSet(STOP, TIDYING)) {// 在此处执行清理工作transitionToTerminated();log.info(线程池已终止);}}}private void transitionToTerminated() {state.set(TERMINATED);// 这里可以通知等待线程池终止的线程} 在线程回收时调用tryTerminate方法尝试转换线程池状态
// 2.跳出循环说明取任务超过了最大等待时间线程歇菜休息吧synchronized (workerSet){workerSet.remove(this);threadTotalNums.decrementAndGet(); //计数扣减}log.info(工作线程》线程{}已被回收当前线程数:{}, Thread.currentThread(), threadTotalNums.get());tryTerminate(); // 尝试转换线程池状态
5.测试
我们设置线程池不允许回收核心线程然后在添加任务后直接调用shutdownNow立即停止线程 public static void main(String[] args){ThreadPool threadPool new ThreadPool(new WorkQueue(5), 2, 5,5L, TimeUnit.SECONDS,(queue, task) - {log.info(拒绝策略》拒绝策略触发直接丢弃当前任务);}, new DefaultThreadFactory());threadPool.setAllowCoreThreadTimeOut(false);for (int i 0; i 15; i) {int finalI i;threadPool.execute(() - {log.info(执行任务{}-------当前执行线程为{} , finalI, Thread.currentThread().toString());});}threadPool.shutdownNow();}
运行结果如下可以看到任务并没有全部执行完全部线程就被回收了
---------------线程池创建成功--------------
最大核心线程数2
最大总线程数5
线程最大空闲时间5
空闲时间单位SECONDS
allowCoreThreadTimeOutfalse
----------------------------------------
21:11:30.648 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:11:30.648 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:11:30.648 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:11:30.649 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:11:30.649 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:11:30.649 [pool-1-thread-1] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-1,5,main]开始运行
21:11:30.649 [pool-1-thread-1] INFO com.luckysj.threadpool.MainTest - 执行任务0-------当前执行线程为Thread[pool-1-thread-1,5,main]
21:11:30.649 [pool-1-thread-2] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-2,5,main]开始运行
21:11:30.649 [pool-1-thread-2] INFO com.luckysj.threadpool.MainTest - 执行任务1-------当前执行线程为Thread[pool-1-thread-2,5,main]
21:11:30.649 [pool-1-thread-4] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-4,5,main]开始运行
21:11:30.649 [pool-1-thread-1] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-1,5,main]任务拿取成功
21:11:30.649 [pool-1-thread-3] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-3,5,main]开始运行
21:11:30.649 [pool-1-thread-4] INFO com.luckysj.threadpool.MainTest - 执行任务8-------当前执行线程为Thread[pool-1-thread-4,5,main]
21:11:30.649 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:11:30.649 [pool-1-thread-1] INFO com.luckysj.threadpool.MainTest - 执行任务2-------当前执行线程为Thread[pool-1-thread-1,5,main]
21:11:30.649 [pool-1-thread-3] INFO com.luckysj.threadpool.MainTest - 执行任务7-------当前执行线程为Thread[pool-1-thread-3,5,main]
21:11:30.649 [pool-1-thread-2] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-2,5,main]任务拿取成功
21:11:30.649 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:11:30.649 [pool-1-thread-2] INFO com.luckysj.threadpool.MainTest - 执行任务3-------当前执行线程为Thread[pool-1-thread-2,5,main]
21:11:30.649 [main] INFO com.luckysj.threadpool.MainTest - 拒绝策略》拒绝策略触发直接丢弃当前任务
21:11:30.649 [pool-1-thread-4] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-4,5,main]任务拿取成功
21:11:30.649 [pool-1-thread-4] INFO com.luckysj.threadpool.MainTest - 执行任务4-------当前执行线程为Thread[pool-1-thread-4,5,main]
21:11:30.649 [pool-1-thread-1] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-1,5,main]任务拿取成功
21:11:30.649 [pool-1-thread-5] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-5,5,main]开始运行
21:11:30.649 [pool-1-thread-1] INFO com.luckysj.threadpool.MainTest - 执行任务5-------当前执行线程为Thread[pool-1-thread-1,5,main]
21:11:30.649 [pool-1-thread-3] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-3,5,main]任务拿取成功
21:11:30.649 [pool-1-thread-5] INFO com.luckysj.threadpool.MainTest - 执行任务10-------当前执行线程为Thread[pool-1-thread-5,5,main]
21:11:30.649 [pool-1-thread-5] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-5,5,main]任务拿取成功
21:11:30.649 [pool-1-thread-3] INFO com.luckysj.threadpool.MainTest - 执行任务6-------当前执行线程为Thread[pool-1-thread-3,5,main]
21:11:30.649 [pool-1-thread-5] INFO com.luckysj.threadpool.MainTest - 执行任务9-------当前执行线程为Thread[pool-1-thread-5,5,main]
21:11:30.649 [pool-1-thread-3] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-3,5,main]任务拿取成功
21:11:30.649 [pool-1-thread-3] INFO com.luckysj.threadpool.MainTest - 执行任务11-------当前执行线程为Thread[pool-1-thread-3,5,main]
21:11:30.649 [pool-1-thread-2] DEBUG com.luckysj.threadpool.core.WorkQueue - 等待队列》Thread[pool-1-thread-2,5,main]线程等待获取任务
21:11:30.649 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:11:30.649 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:11:30.649 [main] INFO com.luckysj.threadpool.core.ThreadPool - 线程池立即关闭尝试中断所有线程
21:11:30.649 [pool-1-thread-4] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-4,5,main]任务拿取成功
21:11:30.649 [pool-1-thread-4] INFO com.luckysj.threadpool.MainTest - 执行任务13-------当前执行线程为Thread[pool-1-thread-4,5,main]
21:11:30.649 [pool-1-thread-1] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-1,5,main]任务拿取成功
21:11:30.649 [main] INFO com.luckysj.threadpool.core.ThreadPool - 线程池已终止
21:11:30.649 [pool-1-thread-1] INFO com.luckysj.threadpool.MainTest - 执行任务14-------当前执行线程为Thread[pool-1-thread-1,5,main]
21:11:30.649 [pool-1-thread-5] DEBUG com.luckysj.threadpool.core.WorkQueue - 等待队列》Thread[pool-1-thread-5,5,main]线程等待获取任务
21:11:30.649 [pool-1-thread-4] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-4,5,main]已被回收当前线程数:4
21:11:30.649 [pool-1-thread-1] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-1,5,main]已被回收当前线程数:3
21:11:30.649 [pool-1-thread-3] DEBUG com.luckysj.threadpool.core.WorkQueue - 等待队列》Thread[pool-1-thread-3,5,main]线程等待获取任务
21:11:30.649 [pool-1-thread-5] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-5,5,main]已被回收当前线程数:2
21:11:30.649 [pool-1-thread-3] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-3,5,main]已被回收当前线程数:1
21:11:30.650 [pool-1-thread-2] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-2,5,main]已被回收当前线程数:0
我们将shutdownNow方法改为shutdown运行结果如下可以看到全部任务执行完成后才开始回收核心线程
21:12:08.248 [main] INFO com.luckysj.threadpool.core.ThreadPool -
---------------线程池创建成功--------------
最大核心线程数2
最大总线程数5
线程最大空闲时间5
空闲时间单位SECONDS
allowCoreThreadTimeOutfalse
----------------------------------------
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [pool-1-thread-1] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-1,5,main]开始运行
21:12:08.252 [pool-1-thread-2] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-2,5,main]开始运行
21:12:08.252 [pool-1-thread-1] INFO com.luckysj.threadpool.MainTest - 执行任务0-------当前执行线程为Thread[pool-1-thread-1,5,main]
21:12:08.252 [pool-1-thread-2] INFO com.luckysj.threadpool.MainTest - 执行任务1-------当前执行线程为Thread[pool-1-thread-2,5,main]
21:12:08.252 [pool-1-thread-1] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-1,5,main]任务拿取成功
21:12:08.252 [pool-1-thread-1] INFO com.luckysj.threadpool.MainTest - 执行任务2-------当前执行线程为Thread[pool-1-thread-1,5,main]
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [pool-1-thread-3] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-3,5,main]开始运行
21:12:08.252 [pool-1-thread-3] INFO com.luckysj.threadpool.MainTest - 执行任务7-------当前执行线程为Thread[pool-1-thread-3,5,main]
21:12:08.252 [pool-1-thread-5] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-5,5,main]开始运行
21:12:08.252 [pool-1-thread-4] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》工作线程Thread[pool-1-thread-4,5,main]开始运行
21:12:08.252 [pool-1-thread-5] INFO com.luckysj.threadpool.MainTest - 执行任务9-------当前执行线程为Thread[pool-1-thread-5,5,main]
21:12:08.252 [pool-1-thread-4] INFO com.luckysj.threadpool.MainTest - 执行任务8-------当前执行线程为Thread[pool-1-thread-4,5,main]
21:12:08.252 [pool-1-thread-3] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-3,5,main]任务拿取成功
21:12:08.252 [pool-1-thread-3] INFO com.luckysj.threadpool.MainTest - 执行任务3-------当前执行线程为Thread[pool-1-thread-3,5,main]
21:12:08.252 [pool-1-thread-2] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-2,5,main]任务拿取成功
21:12:08.252 [pool-1-thread-2] INFO com.luckysj.threadpool.MainTest - 执行任务4-------当前执行线程为Thread[pool-1-thread-2,5,main]
21:12:08.252 [pool-1-thread-1] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-1,5,main]任务拿取成功
21:12:08.252 [pool-1-thread-1] INFO com.luckysj.threadpool.MainTest - 执行任务5-------当前执行线程为Thread[pool-1-thread-1,5,main]
21:12:08.252 [pool-1-thread-5] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-5,5,main]任务拿取成功
21:12:08.252 [pool-1-thread-5] INFO com.luckysj.threadpool.MainTest - 执行任务6-------当前执行线程为Thread[pool-1-thread-5,5,main]
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [pool-1-thread-4] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-4,5,main]任务拿取成功
21:12:08.252 [pool-1-thread-3] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-3,5,main]任务拿取成功
21:12:08.252 [pool-1-thread-4] INFO com.luckysj.threadpool.MainTest - 执行任务10-------当前执行线程为Thread[pool-1-thread-4,5,main]
21:12:08.252 [pool-1-thread-3] INFO com.luckysj.threadpool.MainTest - 执行任务11-------当前执行线程为Thread[pool-1-thread-3,5,main]
21:12:08.252 [pool-1-thread-2] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-2,5,main]任务拿取成功
21:12:08.252 [pool-1-thread-2] INFO com.luckysj.threadpool.MainTest - 执行任务12-------当前执行线程为Thread[pool-1-thread-2,5,main]
21:12:08.252 [pool-1-thread-1] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-1,5,main]任务拿取成功
21:12:08.252 [pool-1-thread-1] INFO com.luckysj.threadpool.MainTest - 执行任务13-------当前执行线程为Thread[pool-1-thread-1,5,main]
21:12:08.252 [pool-1-thread-5] DEBUG com.luckysj.threadpool.core.WorkQueue - 等待队列》Thread[pool-1-thread-5,5,main]线程等待获取任务
21:12:08.252 [main] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》任务添加成功
21:12:08.252 [main] INFO com.luckysj.threadpool.core.ThreadPool - 线程池正在关闭
21:12:08.252 [pool-1-thread-4] INFO com.luckysj.threadpool.core.WorkQueue - 等待队列》线程Thread[pool-1-thread-4,5,main]任务拿取成功
21:12:08.253 [pool-1-thread-4] INFO com.luckysj.threadpool.MainTest - 执行任务14-------当前执行线程为Thread[pool-1-thread-4,5,main]
21:12:08.253 [pool-1-thread-3] DEBUG com.luckysj.threadpool.core.WorkQueue - 等待队列》Thread[pool-1-thread-3,5,main]线程等待获取任务
21:12:08.253 [pool-1-thread-2] DEBUG com.luckysj.threadpool.core.WorkQueue - 等待队列》Thread[pool-1-thread-2,5,main]线程等待获取任务
21:12:08.253 [pool-1-thread-4] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-4,5,main]已被回收当前线程数:4
21:12:08.253 [pool-1-thread-1] DEBUG com.luckysj.threadpool.core.WorkQueue - 等待队列》Thread[pool-1-thread-1,5,main]线程等待获取任务
21:12:08.253 [pool-1-thread-5] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-5,5,main]已被回收当前线程数:3
21:12:08.253 [pool-1-thread-5] INFO com.luckysj.threadpool.core.ThreadPool - 线程池已终止
21:12:08.253 [pool-1-thread-1] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-1,5,main]已被回收当前线程数:0
21:12:08.253 [pool-1-thread-2] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-2,5,main]已被回收当前线程数:1
21:12:08.253 [pool-1-thread-3] INFO com.luckysj.threadpool.core.ThreadPool - 工作线程》线程Thread[pool-1-thread-3,5,main]已被回收当前线程数:2
6.总结
本章节我们简单实现了线程池内部状态的流转这个实现是一个非常基础的版本方便提供小伙伴们学习和思考如果有什么疑问或者建议欢迎评论区指出我们下次再见咕咕咕(又可以鸽几天了)。