threadPoolExecutor的参数和拒绝策略
package com.example.demo; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ThreadpoolExecutorTest { /** * 5+2 没用到maximunpoolsize 用到了corePoolSize * 8+2 没用到了corePoolSize 用到了maximunpoolsize * 大于10 报错 ,这个错看拒绝策略 * @param args */ public static void main(String[] args) { // 创建线程池 , 参数含义 :(核心线程数,最大线程数,加开线程的存活时间,时间单位,任务队列长度) ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 8, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(2)); pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy()); //设置:任务数 = 3 ~ 11 ,分析:任务数 与 活跃线程数,核心线程数,队列长度,最大线程数的关系。 int a = 11; for (int i = 1; i <= a; i++) { int j = i; pool.submit(new Runnable() { @Override public void run() { //获取线程名称 Thread thread = Thread.currentThread(); String name = thread.getName(); //输出 int activeCount = pool.getActiveCount(); System.out.println("任务:" + j + "-----,线程名称:" + name + "-----活跃线程数:" + activeCount); } }); } //关闭线程池 pool.shutdown(); } }
//将被拒绝的任务放到当前调用线程池的线程中去执行
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); } } }
//默认的拒绝策略,报错,抛弃任务
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()); } }
不恋尘世浮华,不写红尘纷扰