线程池
1.池化技术
程序运行的本质:占用系统资源! 提高程序的使用率,降低我们一个性能消耗
线程池、连接池、内存池、对象池 …
- 为什么要用线程池:线程复用
2.线程池属性
三大方法、七大参数、4种拒绝策略
a.三大方法
package com.coding.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolDemo01 {
public static void main(String[] args) {
// 单例,只能有一个线程!
// ExecutorService threadPool = Executors.newSingleThreadExecutor();
// 固定的线程数
// ExecutorService threadPool = Executors.newFixedThreadPool(8);
// 遇强则强!可伸缩!
ExecutorService threadPool = Executors.newCachedThreadPool();
try {
// 线程池的使用方式!
for (int i = 0; i < 30; i++) {
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName() + " ok");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 使用完毕后需要关闭!
threadPool.shutdown();
}
}
}
b.七大参数
1、先看看这个三个方法的源码
new ThreadPoolExecutor(0, Integer.MAX_VALUE, // 约等于21亿
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>())
public ThreadPoolExecutor(int corePoolSize, // 核心池线程数大小 (常用)
int maximumPoolSize, // 最大的线程数大小 (常用)
long keepAliveTime, // 超时等待时间 (常用)
TimeUnit unit, // 时间单位 (常用)
BlockingQueue<Runnable> workQueue, // 阻塞队列(常用)
ThreadFactory threadFactory, // 线程工厂
RejectedExecutionHandler handler // 拒绝策略(常用)) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
- 自定义线程池的策略
package com.coding.pool;
import java.util.concurrent.*;
public class ThreadPoolDemo01 {
public static void main(String[] args) {
// 单例,只能有一个线程!
// ExecutorService threadPool = Executors.newSingleThreadExecutor();
// 固定的线程数
// ExecutorService threadPool = Executors.newFixedThreadPool(1);
//遇强则强!可伸缩!
// ExecutorService threadPool = Executors.newCachedThreadPool();
ExecutorService threadPool = new ThreadPoolExecutor(
2,
5,
3L,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());
try {
// 线程池的使用方式!
for (int i = 0; i < 100; i++) {
threadPool.execute(()->{
System.out.println(Thread.currentThread().getName() + " ok");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 使用完毕后需要关闭!
threadPool.shutdown();
}
}
}
c.四大拒绝策略
- 1、ThreadPoolExecutor.AbortPolicy(); 抛出异常,丢弃任务
- 2、ThreadPoolExecutor.DiscardPolicy();不抛出异常,丢弃任务
- 3、ThreadPoolExecutor.DiscardOldestPolicy(); 尝试获取任务,不一定执行!
- 4、ThreadPoolExecutor.CallerRunsPolicy(); 哪来的去哪里找对应的线程执行!
请你谈谈 最大线程池 该如何设置啊?
CPU密集型: 根据CPU的处理器数量来定!保证最大效率
IO密集型: 50 个线程都是进程操作大io资源, 比较耗时! > 这个常用的 IO 任务数!