线程池
2021-06-10 21:05
标签:unless uri 操作 pre 取出 大小 pool lex 最大值 线程池:三大方法、七大参数、4种拒绝策略 1、降低资源的消耗 观察上面的三种方法,发现其本质都是使用ThreadPoolExecutor类创建线程池,要了解这个类,就需要知道线程池的七大参数 注意:上面介绍三种方法时,说明了会产生OOM,所以必须要使用ThreadPoolExecutor来创建线程池 线程池有三个重要的参数 核心线程数最大线程数阻塞队列,线程池触发拒绝策略需要这三个参数总体协调,而不是简单超过了最大线程数就触发。
当提交的任务数大于corePoolSize(核心线程数)时,会优先放入到阻塞队列中,只有填满了阻塞队列后,才会判断当前运行的任务是否大于maxPoolSize(线程最大数),小于时会新建线程进行处理。大于时就会触发拒绝策略。总结就是 当提交的任务数 大于 (maxPoolSize + queueCapacity(阻塞队列)) 时就会触发线程池的拒绝策略 线程池 标签:unless uri 操作 pre 取出 大小 pool lex 最大值 原文地址:https://www.cnblogs.com/liuzhidao/p/14243667.html线程池的好处
2、提高响应速度
3、方便管理
线程复用、可以控制最大并发数、管理线程三大方法
ExecutorService threadPool = Executors.newSingleThreadExecutor(); //创建只有单个线程的线程池
ExecutorService threadPool = Executors.newFixedThreadPool(5); //创建固定大小为5的线程池
ExecutorService threadPool = Executors.newCachedThreadPool(); //创建一个可伸缩的线程池
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1, //核心数和最大数都为1
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue
七大参数
public ThreadPoolExecutor(int corePoolSize, //核心线程池大小
int maximumPoolSize, //最大线程池大小
long keepAliveTime, //超时了没有人调用就会释放
TimeUnit unit, // 超时单位
BlockingQueue
线程池触发拒绝策略的时机
四种拒绝策略
简单粗暴,直接抛出拒绝异常,这也是默认的拒绝策略public static class AbortPolicy implements RejectedExecutionHandler {
/**
* Creates an {@code AbortPolicy}.
*/
public AbortPolicy() { }
/**
* Always throws RejectedExecutionException.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
* @throws RejectedExecutionException always
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
如果线程池未关闭,则会在调用者线程中执行新任务,这会导致主线程提交线程性能变慢 public static class CallerRunsPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code CallerRunsPolicy}.
*/
public CallerRunsPolicy() { }
/**
* Executes task r in the caller‘s thread, unless the executor
* has been shut down, in which case the task is discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
不做任何操作,直接丢弃。 public static class DiscardPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardPolicy}.
*/
public DiscardPolicy() { }
/**
* Does nothing, which has the effect of discarding task r.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
抛弃最老的任务,就是从队列取出最老的任务,然后放入新的任务执行。 public static class DiscardOldestPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardOldestPolicy} for the given executor.
*/
public DiscardOldestPolicy() { }
/**
* Obtains and ignores the next task that the executor
* would otherwise execute, if one is immediately available,
* and then retries execution of task r, unless the executor
* is shut down, in which case task r is instead discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
线程池的大小如何设置
上一篇:java中的Object类