java线程池拒绝策略代码篇(三)

目录

写在前面

四种拒绝策略

1:AbortPolicy(抛出异常)

1.1代码

1.2运行结果

2:CallerRunsPolicy(谁调用谁运行)

2.1代码

2.2结果

3:DiscardPolicy(直接丢弃)

3.1代码

3.2结果

4:DiscardOldestPolicy(丢弃老的)

4.1代码

4.2结果


写在前面

当任务个数达到线程池的初始值,后来的任务会放入到线程池的阻塞队列中,当阻塞队列满了后,如果还有任务过来,线程池中线程的数量会动态的扩张直到线程池的最大值,达到最大值后,如果还会任务过来,会启用拒绝策略

四种拒绝策略

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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值