[调优]Future超时阻塞与线程池拒绝策略

一、拒绝策略DiscardOldestPolicy

/**
 * @author: guanjian
 * @date: 2020/06/30 14:39
 * @description:
 */
public class TestThreadPoolRejectDiscardOldestPolicy {
 
    /**
     * 声明一个线程池
     */
    private final static ThreadPoolExecutor executorService = new ThreadPoolExecutor(
            //初始工作线程是1
            1,
            //最大线程也是1,意味着同一时刻只有一个线程可以在执行
            1,
            1L, TimeUnit.MINUTES,
            //阻塞队列只允许一个线程排队
            new ArrayBlockingQueue<>(1),
            //队列排队溢出时,丢弃队首线程
            new ThreadPoolExecutor.DiscardOldestPolicy());
 
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
 
 
        Future f1 = executorService.submit(() -> {
            try {
                System.out.println("f1 thread executing");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        Future f2 = executorService.submit(() -> {
            try {
                System.out.println("f2 thread executing");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        Future f3 = executorService.submit(() -> {
            try {
                System.out.println("f3 thread executing");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        //f1、f2、f3三个线程按顺序提交请求线程池执行
        //f1线程提交,获取执行权,经过5s执行时间,f1.get()返回结果
        //f2线程提交,由于f1执行需要5s,直接进入阻塞队列等待
        //f3线程提交,由于f1在执行,f2在阻塞队列且队列已满,且当前拒绝策略是DiscardOldestPolicy,会把队首排队线程f2弹出
        //f3被提交到阻塞队列中,当f1执行完,f3继续执行;
        //但是f2被弹出,f2.get()同样会一直阻塞,f3即时获取到了执行权也完成了执行,但按照代码顺序f3.get()根本不会得到执行
        //由于future.get()会一直阻塞但线程已经被抛弃没有机会变更状态,因此会一直阻塞
        System.out.printf("f1=%s",f1.get());
        System.out.printf("f2=%s",f2.get());
        System.out.printf("f3=%s",f3.get());
        System.out.println("================end================");
 
        //当使用DiscardOldestPolicy作为线程池拒绝策略时,要进行超时获取避免hang住线程一直阻塞自旋无法释放cpu
        System.out.printf("f1=%s",f1.get(10,TimeUnit.SECONDS));
        System.out.printf("f2=%s",f2.get(10,TimeUnit.SECONDS));
        System.out.printf("f3=%s",f3.get(10,TimeUnit.SECONDS));
        System.out.println("================end================");
    }
}

二、拒绝策略DiscardPolicy

/**
 * @author: guanjian
 * @date: 2020/06/30 14:39
 * @description: 模拟线程池队列满
 */
public class TestThreadPoolRejectDiscardPolicy {
 
    /**
     * 声明一个线程池
     */
    private final static ThreadPoolExecutor executorService = new ThreadPoolExecutor(
            //初始工作线程是1
            1,
            //最大线程也是1,意味着同一时刻只有一个线程可以在执行
            1,
            1L, TimeUnit.MINUTES,
            //阻塞队列只允许一个线程排队
            new ArrayBlockingQueue<>(1),
            //队列排队溢出时,直接抛弃溢出的线程
            new ThreadPoolExecutor.DiscardPolicy());
 
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
 
 
        Future f1 = executorService.submit(() -> {
            try {
                System.out.println("f1 thread executing");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        Future f2 = executorService.submit(() -> {
            try {
                System.out.println("f2 thread executing");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        Future f3 = executorService.submit(() -> {
            try {
                System.out.println("f3 thread executing");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
 
        //f1、f2、f3三个线程按顺序提交请求线程池执行
        //f1线程提交,获取执行权,经过5s执行时间,f1.get()返回结果
        //f2线程提交,由于f1执行需要5s,直接进入阻塞队列等待,f1执行完,f2开始执行
        //f3线程提交,由于f1在执行,f2在阻塞队列且队列已满,f3被当前拒绝策略抛弃,
        //由于future.get()会一直阻塞但线程已经被抛弃没有机会变更状态,因此会一直阻塞
        System.out.printf("f1=%s",f1.get());
        System.out.printf("f2=%s",f2.get());
        System.out.printf("f3=%s",f3.get());
 
        //当使用DiscardPolicy作为线程池拒绝策略时,要进行超时获取避免hang住线程一直阻塞自旋无法释放cpu
        System.out.printf("f1=%s",f1.get(10,TimeUnit.SECONDS));
        System.out.printf("f2=%s",f2.get(10,TimeUnit.SECONDS));
        System.out.printf("f3=%s",f3.get(10,TimeUnit.SECONDS));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大摩羯先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值