线程和线程池
[TOC]
1.new Thread的弊端
- 每次new Thread新建对象,性能差
- 线程缺乏统一管理,可能无限制的新建线程,相互竞争,有可能占用过多系统资源导致死机或者OOM
- 缺少更多功能,如更多执行、定期执行、线程中断
2.线程池的好处
- 重用存在的线程,减少对象创建、消亡的的开销,性能佳
- 可有效控制最大并发线程数,提高系统资源利用率,同时可以避免过多资源竞争,避免阻塞
- 提供定时执行、定期执行、单线程、并发控制等功能
3.线程池相关名称解释
- coreThreadSize:核心线程数量
- maximumPoolSize:最大线程数
- workQueue:阻塞队列,存储等待执行的任务(很重要,会对线程池运行过程产生重大影响)
- keepAliveTime:线程没有任务执行时最多保持多久时间终止
- unit:keepAliveTime的时间单位
- threadFactory:线程工厂,用于创建线程
- rejectHandler:当拒绝处理任务的策略
4.线程和线程方法
shutdown()方法:不接受新的任务,处理完队列中的任务
stop()方法:则是直接中断正在处理任务的线程
5.合理配置线程池
5.1 cpu密集型任务
考虑:N * CPU + 1个
5.2 io密集型任务
考虑:2 * N * CPU个
6.线程池配置使用例子
6.1 配置类 ThreadPoolConfig.class
@Configuration
public class ThreadPoolConfig {
@Data
private static class ThreadPool {
private String threadPoolName = "mall-admin-thread-pool";
private Integer coreThreadSize = Runtime.getRuntime().availableProcessors() * 2;
private Integer maxThreadSize = Runtime.getRuntime().availableProcessors() * 2;
private Long keepAliveTime = 0L;
private Integer capacity = 100;
}
private static ThreadFactory factory = null;
@Bean
public ExecutorService createThreadPool() {
ThreadPool threadPool = new ThreadPool();
factory = setFactory(threadPool.getThreadPoolName());
return new ThreadPoolExecutor(
threadPool.getCoreThreadSize(),
threadPool.getMaxThreadSize(),
threadPool.getKeepAliveTime(),
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1000),
factory,
new MyAbortPolicy());
}
/**
* 设置线程池的名称
* @param poolName
* @return
*/
private static ThreadFactory setFactory(String poolName) {
if (factory == null) {
factory = new ThreadPoolExecutorFactoryBean();
((ThreadPoolExecutorFactoryBean) factory).setBeanName(poolName);
}
return factory;
}
/**
* 自定义AbortPolicy类(即自定义饱和策略的异常处理)
*/
private static class MyAbortPolicy implements RejectedExecutionHandler {
MyAbortPolicy() {
}
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
throw new RejectedExecutionException(
"【线程默认饱和策略】:" + r.toString() + "线程被" + executor.toString() + "线程执行器拒绝");
}
}
}
6.2 使用
@Autowired
private ExecutorService executorService;
...
public void excute(){
// 异步
executorService.execute(() -> doSomething());
}