目录
线程池
线程池促发拒绝策略的过程:线程池创建核心线程数corePoolSize,当有更多线程过来,放到缓存队列workQueue,队列满了之后,大于最大线程数maximumPoolSize,最后促发拒绝策略。保存活着时间是指线程池里面的线程空闲的时间maximumPoolSize。
4种拒绝策略
CallerRunsPolicy,AbortPolicy,DiscardPolicy,DiscardOldestPolicy
Demo
import lombok.extern.slf4j.Slf4j;
/**
* Created on 2019/8/30.
*
* @author yangsen
*/
@Slf4j
public class TestRunnable implements Runnable {
@Override
public void run() {
if ("main".equals(Thread.currentThread().getName())) {
log.info("main线程出现次数:{}", ThreadTest.sum.incrementAndGet());
}
log.info("线程名称:{},正在运行", Thread.currentThread().getName());
}
}
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
/**
* Created on 2019/8/30.
*
* @author yangsen
*/
public class ThreadTest {
static AtomicLong sum = new AtomicLong(0);
public static void main(String[] args) throws InterruptedException {
/**
* 调用者运行策略CallerRunsPolicy
*/
// ThreadPoolExecutor executor=new ThreadPoolExecutor(2,5,5, TimeUnit.SECONDS,new LinkedBlockingDeque<>(2),new ThreadPoolExecutor.CallerRunsPolicy());
/**
* 中止策略AbortPolicy
*/
//ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2), new ThreadPoolExecutor.AbortPolicy());
/**
* 丢弃策略DiscardPolicy
*/
//ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2), new ThreadPoolExecutor.DiscardPolicy());
/**
* 弃老策略DiscardOldestPolicy
*/
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2), new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 0; i < 100; i++) {
executor.execute(new TestRunnable());
}
}
}
调用者运行策略CallerRunsPolicy
ThreadPoolExecutor executor=new ThreadPoolExecutor(2,5,5, TimeUnit.SECONDS,new LinkedBlockingDeque<>(2),new ThreadPoolExecutor.CallerRunsPolicy());
运行结果
10:42:39.790 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.790 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.790 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.795 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.790 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.790 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.790 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:1
10:42:39.795 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:2
10:42:39.795 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.795 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.795 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.795 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:3
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:4
10:42:39.795 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.795 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:5
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.795 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:6
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.795 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:7
10:42:39.795 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.795 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.795 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.795 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:8
10:42:39.795 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.795 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:9
10:42:39.795 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.795 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.795 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:10
10:42:39.796 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:11
10:42:39.796 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.796 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.796 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:12
10:42:39.796 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:13
10:42:39.796 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.796 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:14
10:42:39.796 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:15
10:42:39.796 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.796 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:16
10:42:39.796 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:17
10:42:39.796 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.796 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.796 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:18
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:19
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:20
10:42:39.796 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.796 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:21
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.796 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:22
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:23
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:24
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:25
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.797 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.797 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.797 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.797 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:26
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:27
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:28
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:29
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:30
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:31
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:32
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
10:42:39.797 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.797 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:42:39.797 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:42:39.797 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - main线程出现次数:33
10:42:39.797 [main] INFO com.example.demo.test.TestRunnable - 线程名称:main,正在运行
结论
可以看到线程是不会中断的,在main线程调用,难怪叫调用者运行策略,main线程其中出现了33次。
中止策略AbortPolicy
默认策略
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2), new ThreadPoolExecutor.AbortPolicy());
运行结果
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.example.demo.test.TestRunnable@2cdf8d8a rejected from java.util.concurrent.ThreadPoolExecutor@30946e09[Running, pool size = 5, active threads = 5, queued tasks = 2, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at com.example.demo.test.ThreadTest.main(ThreadTest.java:28)
10:51:34.747 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:51:34.747 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:51:34.747 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:51:34.748 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:51:34.747 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:51:34.752 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
10:51:34.752 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
结论
由于创建的时候根本没有停顿,瞬间就爆了,线程也中断了。所以要处理好抛出的异常,进行下面的改造。
for (int i = 0; i < 100; i++) {
try {
executor.execute(new TestRunnable());
}catch (Exception e){
log.error("出现错误:{}",e.getMessage());
}
}
忽然的顿悟
由于这个是默认策略,ExecutorService 的队列是无界的,所以当线程数提交很多时,会把整个队列塞满,导致cpu很高,所以一般使用ThreadPoolExecutor去创建线程池!!!nice
丢弃策略DiscardPolicy
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2), new ThreadPoolExecutor.DiscardPolicy());
运行结果
10:57:25.292 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:57:25.296 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:57:25.296 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
10:57:25.309 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
10:57:25.309 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
10:57:25.312 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
10:57:25.316 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
结论
也是由于没有停顿,瞬间爆了,然后把新加入的都剔除掉
弃老策略DiscardOldestPolicy
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 5, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2), new ThreadPoolExecutor.DiscardOldestPolicy());
运行结果
11:37:25.531 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
11:37:25.531 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
11:37:25.535 [pool-1-thread-1] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-1,正在运行
11:37:25.531 [pool-1-thread-2] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-2,正在运行
11:37:25.531 [pool-1-thread-5] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-5,正在运行
11:37:25.531 [pool-1-thread-3] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-3,正在运行
11:37:25.535 [pool-1-thread-4] INFO com.example.demo.test.TestRunnable - 线程名称:pool-1-thread-4,正在运行
结果
会把老的线程剔除。
可能会再出一篇ExecutorService的总结