由于频繁创建销毁线程要调用native方法比较消耗资源,为了保证内核的充分利用,所以引入了线程池的概念。
📌 线程池优点
降低资源消耗
提高响应速度
方便管理
📌 创建线程池
使用Executors创建
使用ThreadPoolExecutor创建
📌 工作流程
📌 三大方法
ExecutorService threadPool = Executors.newSingleThreadExecutor();--创建单个线程
ExecutorService threadPool = Executors.newFixedThreadPool(5);--创建固定大小的线程池
ExecutorService threadPool = Executors.newCachedThreadPool();--创建不固定大小的线程池
📌 代码举例
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ExecutorsDemo {public static void main(String[] args) {ExecutorService threadPool = Executors.newSingleThreadExecutor();
// ExecutorService threadPool = Executors.newFixedThreadPool(5);
// ExecutorService threadPool = Executors.newCachedThreadPool();try {//使用线程池创建线程池for (int i = 0; i < 5; i++) {threadPool.execute(()->{System.out.println(Thread.currentThread().getName()+" Ok");});}} catch (Exception e) {e.printStackTrace();} finally {threadPool.shutdown();}}
}
📌 七大参数说明
corePoolSize:核心线程数量,线程池维护线程的最少数量
maximumPoolSize:线程池维护线程的最大数量
keepAliveTime:线程池除核心线程外的其他线程的最长空闲时间,超过该时间的空闲线程会被销毁
unit:keepAliveTime的单位,TimeUnit中的几个静态属性:NANOSECONDS、MICROSECONDS、MILLISECONDS、SECONDS
workQueue:线程池所使用的任务缓冲队列
threadFactory:线程工厂,用于创建线程,一般用默认的即可
handler:线程池对拒绝任务的处理策略
📢 官方推荐ThreadPoolExecutor创建线程池
阿里代码规范如下:
源码解析👇
//1.newSingleThreadExecutor
public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue()));
}
//2.newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue());
}
//3.newCachedThreadPool
public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE, //21亿60L, TimeUnit.SECONDS,new SynchronousQueue());
}public ThreadPoolExecutor(int corePoolSize, // 核心线程数int maximumPoolSize, // 最大线程数 long keepAliveTime, // 存活时间TimeUnit unit, // 存活时间单位BlockingQueue workQueue, // 阻塞队列ThreadFactory threadFactory, // 线程工厂RejectedExecutionHandler handler //拒绝策略) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,threadFactory, defaultHandler);
}
可以看到Executors创建线程池的三个方法都调用了ThreadPoolExecutor!
📌 UML图
📌 要点
AbortPolicy:被拒绝了抛出异常
CallerRunsPolicy:使用调用者所在线程执行,就是哪里来的回哪里去
DiscardOldestPolicy: 当触发拒绝策略,只要线程池没有关闭的话,丢弃阻塞队列 workQueue 中最老的一个任务,并将新任务加入
DiscardPolicy:直接丢弃,其他啥都没有
ThreadPoolExecutor 默认AbortPolicy拒绝策略
public class AbortPolicyDemo {public static void main(String[] args) {//自定义线程池!ExecutorService threadPool = new ThreadPoolExecutor(3,//核心线程为35,//最大线程数是5,包括核心线程10,TimeUnit.SECONDS,new LinkedBlockingQueue<>(3),//阻塞队列数Executors.defaultThreadFactory());try {for (int i = 0; i < 10; i++) {threadPool.execute(()->{System.out.println(Thread.currentThread().getName()+" OK");});}} catch (Exception e) {e.printStackTrace();} finally {threadPool.shutdown();}}
}
控制台输出:
public class CallerRunsPolicyDemo {public static void main(String[] args) {//自定义线程池!ExecutorService threadPool = new ThreadPoolExecutor(3,//核心线程为35,//最大线程数是5,包括核心线程10,TimeUnit.SECONDS,new LinkedBlockingQueue<>(3),//阻塞队列数Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());try {for (int i = 0; i < 10; i++) {threadPool.execute(()->{System.out.println(Thread.currentThread().getName()+" OK");});}} catch (Exception e) {e.printStackTrace();} finally {threadPool.shutdown();}}
}
控制台输出:
📢 可以看到,多出来的两个线程回到了主线程,因为这10个线程都是从主线程丢到线程池的。
它会尝试丢掉任务队列中的最早任务,也不会抛出异常。
public class DiscardOldestPolicyDemo {public static void main(String[] args) {//自定义线程池!ExecutorService threadPool = new ThreadPoolExecutor(3,//核心线程为35,//最大线程数是5,包括核心线程10,TimeUnit.SECONDS,new LinkedBlockingQueue<>(3),//阻塞队列数Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardOldestPolicy());try {for (int i = 0; i < 8; i++) {threadPool.execute(()->{try {//添加等待时间,防止过早结束TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+" OK");});}//第九个线程,他会尝试插入,替代最老的线程threadPool.execute(()->{System.out.println(Thread.currentThread().getName()+" OK11");});} catch (Exception e) {e.printStackTrace();} finally {threadPool.shutdown();}}
}
控制台输出:
📢 可以看到总数还是8个线程,而且“0K11”输出了,说明其中一个线程被顶掉了。
public class DiscardPolicyDemo {public static void main(String[] args) {//自定义线程池!ExecutorService threadPool = new ThreadPoolExecutor(3,//核心线程为35,//最大线程数是5,包括核心线程10,TimeUnit.SECONDS,new LinkedBlockingQueue<>(3),//阻塞队列数Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardPolicy());try {for (int i = 0; i < 8; i++) {threadPool.execute(()->{try {//添加等待时间,防止过早结束TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName()+" OK");});}//第九个线程,他会尝试插入,替代最老的线程threadPool.execute(()->{System.out.println(Thread.currentThread().getName()+" OK11");});} catch (Exception e) {e.printStackTrace();} finally {threadPool.shutdown();}}
}
控制台输出:
📢 第9个线程被丢掉了!
上一篇:sklearn使用入门
下一篇:网络安全之资产及攻击面管理