Java 线程池四种拒绝策略

四种拒绝策略:

拒绝策略类型说明
ThreadPoolExecutor.AbortPolicy默认拒绝策略,拒绝任务并抛出任务
ThreadPoolExecutor.CallerRunsPolicy使用调用线程直接运行任务
ThreadPoolExecutor.DiscardPolicy直接拒绝任务,不抛出错误
ThreadPoolExecutor.DiscardOldestPolicy触发拒绝策略,只要还有任务新增,一直会丢弃阻塞队列的最老的任务,并将新的任务加入

验证:

package com.thread.test;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangcj
 * @date 2023/5/24 14:29
 */
public class TestThreadPoolExecutor {

    public static void main(String[] args) {
		//默认拒绝策略,拒绝任务并抛出任务
        ThreadPoolExecutor.AbortPolicy abortPolicy =  new ThreadPoolExecutor.AbortPolicy();
        //使用调用线程直接运行任务
        ThreadPoolExecutor.CallerRunsPolicy callerRunsPolicy =  new ThreadPoolExecutor.CallerRunsPolicy();
        //直接拒绝任务,不抛出错误
        ThreadPoolExecutor.DiscardPolicy discardPolicy =  new ThreadPoolExecutor.DiscardPolicy();
        //触发拒绝策略,只要还有任务新增,一直会丢弃阻塞队列的最老的任务,并将新的任务加入
        ThreadPoolExecutor.DiscardOldestPolicy discardOldestPolicy =  new ThreadPoolExecutor.DiscardOldestPolicy();
      ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1),abortPolicy );
        // 任务1
        pool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3 * 1000);
                    System.out.println("-------------hello world_001---------------" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // 任务2
        pool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(5 * 1000);
                    System.out.println("-------------hello world_002---------------" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        // 任务3
        pool.execute(new Runnable() {

            @Override
            public void run() {
                System.out.println("-------------hello world_003---------------" + Thread.currentThread().getName());
            }
        });

        // 任务4
        pool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(2 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("-------------hello world_004---------------" + Thread.currentThread().getName());
            }
        });

        // 任务5
        pool.execute(new Runnable() {

            @Override
            public void run() {
                System.out.println("-------------hello world_005---------------" + Thread.currentThread().getName());
            }
        });

    }

}

1、ThreadPoolExecutor.AbortPolicy 默认拒绝策略,拒绝任务并抛出任务(日志输出)

Connected to the target VM, address: '127.0.0.1:56382', transport: 'socket'
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.thread.test.TestThreadPoolExecutor$5@277c0f21 rejected from java.util.concurrent.ThreadPoolExecutor@7a46a697[Running, pool size = 3, active threads = 3, queued tasks = 1, 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.thread.test.TestThreadPoolExecutor.main(TestThreadPoolExecutor.java:73)
-------------helloworld_004---------------pool-1-thread-3
-------------helloworld_003---------------pool-1-thread-3
-------------helloworld_001---------------pool-1-thread-1
-------------helloworld_002---------------pool-1-thread-2

2、ThreadPoolExecutor.CallerRunsPolicy 使用调用线程直接运行任务(日志输出)

Connected to the target VM, address: '127.0.0.1:56109', transport: 'socket'
-------------helloworld_005---------------main
-------------helloworld_004---------------pool-1-thread-3
-------------helloworld_003---------------pool-1-thread-3
-------------helloworld_001---------------pool-1-thread-1
-------------helloworld_002---------------pool-1-thread-2

3、ThreadPoolExecutor.DiscardPolicy 直接拒绝任务,不抛出错误(日志输出)

Connected to the target VM, address: '127.0.0.1:56421', transport: 'socket'
-------------helloworld_004---------------pool-1-thread-3
-------------helloworld_003---------------pool-1-thread-3
-------------helloworld_001---------------pool-1-thread-1
-------------helloworld_002---------------pool-1-thread-2

4、ThreadPoolExecutor.DiscardOldestPolicy 触发拒绝策略,只要还有任务新增,一直会丢弃阻塞队列的最老的任务,并将新的任务加入(日志输出)

package com.thread.test;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangcj
 * @date 2023/5/24 14:29
 */
public class TestThreadPoolExecutor {

    public static void main(String[] args) {
        //默认拒绝策略,拒绝任务并抛出任务
        ThreadPoolExecutor.AbortPolicy abortPolicy =  new ThreadPoolExecutor.AbortPolicy();
        //使用调用线程直接运行任务
        ThreadPoolExecutor.CallerRunsPolicy callerRunsPolicy =  new ThreadPoolExecutor.CallerRunsPolicy();
        //直接拒绝任务,不抛出错误
        ThreadPoolExecutor.DiscardPolicy discardPolicy =  new ThreadPoolExecutor.DiscardPolicy();
        //触发拒绝策略,只要还有任务新增,一直会丢弃阻塞队列的最老的任务,并将新的任务加入
        ThreadPoolExecutor.DiscardOldestPolicy discardOldestPolicy =  new ThreadPoolExecutor.DiscardOldestPolicy();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 3, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1),discardOldestPolicy);
        // 任务1
        pool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(9 * 1000);
                    System.out.println("-------------helloworld_001---------------" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // 任务2
        pool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(5 * 1000);
                    System.out.println("-------------helloworld_002---------------" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        // 任务3
        pool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(3 * 1000);
                    System.out.println("-------------helloworld_003---------------" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });

        // 任务4
        pool.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(2 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("-------------helloworld_004---------------" + Thread.currentThread().getName());
            }
        });

        // 任务5
        pool.execute(new Runnable() {

            @Override
            public void run() {
                System.out.println("-------------helloworld_005---------------" + Thread.currentThread().getName());
            }
        });

        // 任务6
        pool.execute(new Runnable() {

            @Override
            public void run() {
                System.out.println("-------------helloworld_006---------------" + Thread.currentThread().getName());
            }
        });

    }

}

日志输出:

Connected to the target VM, address: '127.0.0.1:56598', transport: 'socket'
-------------helloworld_004---------------pool-1-thread-3
-------------helloworld_006---------------pool-1-thread-3
-------------helloworld_002---------------pool-1-thread-2
-------------helloworld_001---------------pool-1-thread-1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值