目录
写在前面
当任务个数达到线程池的初始值,后来的任务会放入到线程池的阻塞队列中,当阻塞队列满了后,如果还有任务过来,线程池中线程的数量会动态的扩张直到线程池的最大值,达到最大值后,如果还会任务过来,会启用拒绝策略
四种拒绝策略
1:AbortPolicy(抛出异常)
抛异常的
1.1代码
@Test
public void testMyThreadPool(){
ExecutorService threadPool = new ThreadPoolExecutor(3, 5,
2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
for (int i = 1; i <= 80; i++) {
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "\t 工作");
}
});
}
}
1.2运行结果
pool-1-thread-1 工作
pool-1-thread-2 工作
pool-1-thread-3 工作
pool-1-thread-3 工作
pool-1-thread-2 工作
pool-1-thread-3 工作
pool-1-thread-1 工作
pool-1-thread-2 工作
pool-1-thread-2 工作
pool-1-thread-3 工作
pool-1-thread-4 工作
pool-1-thread-1 工作
pool-1-thread-5 工作
pool-1-thread-3 工作
pool-1-thread-2 工作
java.util.concurrent.RejectedExecutionException: Task com.zcc.TestThreadPool$4@6d4b1c02 rejected from java.util.concurrent.ThreadPoolExecutor@5622fdf[Running, pool size = 5, active threads = 0, queued tasks = 0, completed tasks = 15]
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)
2:CallerRunsPolicy(谁调用谁运行)
2.1代码
@Test
public void testMyThreadPool(){
Thread.currentThread().setName("我是主线程---调用线程");
ExecutorService threadPool = new ThreadPoolExecutor(3, 5,
2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy()
);
for (int i = 1; i <= 80; i++) {
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "\t 工作");
}
});
}
}
2.2结果
“我是主线程---调用线程 工作” ,因为当前线程池是由我是主线程---调用线程线程调用的
pool-1-thread-1 工作
pool-1-thread-2 工作
pool-1-thread-3 工作
pool-1-thread-1 工作
pool-1-thread-2 工作
pool-1-thread-3 工作
pool-1-thread-3 工作
pool-1-thread-2 工作
pool-1-thread-4 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-2 工作
pool-1-thread-3 工作
pool-1-thread-5 工作
我是主线程---调用线程 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-3 工作
我是主线程---调用线程 工作
我是主线程---调用线程 工作
pool-1-thread-1 工作
pool-1-thread-3 工作
我是主线程---调用线程 工作
pool-1-thread-4 工作
pool-1-thread-1 工作
pool-1-thread-3 工作
pool-1-thread-2 工作
pool-1-thread-4 工作
我是主线程---调用线程 工作
我是主线程---调用线程 工作
pool-1-thread-5 工作
3:DiscardPolicy(直接丢弃)
3.1代码
@Test
public void testMyThreadPool(){
ExecutorService threadPool = new ThreadPoolExecutor(3, 5,
2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardPolicy()
);
for (int i = 1; i <= 20; i++) {
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "\t 工作");
}
});
}
}
3.2结果
放入了20个任务,只执行力15个任务,后来的直接丢弃了
pool-1-thread-1 工作
pool-1-thread-2 工作
pool-1-thread-3 工作
pool-1-thread-3 工作
pool-1-thread-1 工作
pool-1-thread-2 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-3 工作
pool-1-thread-4 工作
pool-1-thread-4 工作
pool-1-thread-4 工作
pool-1-thread-2 工作
pool-1-thread-5 工作
4:DiscardOldestPolicy(丢弃老的)
4.1代码
@Test
public void testMyThreadPool(){
ExecutorService threadPool = new ThreadPoolExecutor(3, 5,
2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardOldestPolicy()
);
for (int i = 1; i <= 20; i++) {
threadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "\t 工作");
}
});
}
}
4.2结果
放入20 个,执行了12个
pool-1-thread-1 工作
pool-1-thread-2 工作
pool-1-thread-3 工作
pool-1-thread-1 工作
pool-1-thread-1 工作
pool-1-thread-4 工作
pool-1-thread-1 工作
pool-1-thread-2 工作
pool-1-thread-2 工作
pool-1-thread-2 工作
pool-1-thread-2 工作
pool-1-thread-5 工作
java线程池详细讲解代码篇(二) https://blog.csdn.net/weixin_45191798/article/details/100159321