• Java多线程4种拒绝策略


    一、简介

    Java多线程编程中,我们通常使用线程池来管理和调度任务。线程池由一组预先创建的线程组成,可以重复利用这些线程来执行多个任务,避免频繁地创建和销毁线程而带来的性能开销。

    线程池中的任务队列已满且无法再接受新的任务时,就需要采取拒绝策略来处理这种情况。拒绝策略定义了当无法再接受新的任务时如何处理这些被拒绝的任务。

    Java提供了四种常见的拒绝策略:

    1. AbortPolicy(抛出异常):默认的拒绝策略。当任务无法被提交给线程池时,会直接抛出RejectedExecutionException异常。

    2. CallerRunsPolicy(调用者运行):当任务无法被提交给线程池时,会由提交任务的线程自己执行该任务。

    3. DiscardPolicy(直接丢弃):当任务无法被提交给线程池时,直接丢弃该任务,没有任何提示或处理。

    4. DiscardOldestPolicy(丢弃最旧任务):当任务无法被提交给线程池时,会丢弃队列中最早的一个任务,然后尝试再次提交当前任务。

    二、AbortPolicy拒绝策略

    A. 概述

    AbortPolicy是ThreadPoolExecutor的默认拒绝策略,当任务无法被提交给线程池时,会直接抛出RejectedExecutionException异常。

    B. 拒绝策略实现原理

    实现RejectedExecutionHandler接口,在rejectedExecution方法中抛出异常。

    public class AbortPolicy implements RejectedExecutionHandler {
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    C. 应用场景

    适用于对任务提交失败要求敏感的场景,需要明确知道任务是否被接受并执行。

    D. 使用示例

    当线程池的任务队列和线程队列都已满的情况下执行决绝策略

    public class Task implements Runnable {
    
        private final int index;
    
        public Task(int index) {
            this.index = index;
        }
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + ":" + index);
        }
    }
    
    
    public class Main {
    
        public static void main(String[] args) {
            // 创建线程池
            ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 0L,
                    TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(1),
                    new ThreadPoolExecutor.AbortPolicy());
            try {
                // 提交任务
                threadPool.execute(new Task(1));
                threadPool.execute(new Task(2));
                threadPool.execute(new Task(3));
            } catch (RejectedExecutionException e) {
                e.printStackTrace();
            } finally {
                // 关闭线程池
                threadPool.shutdown();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    三、CallerRunsPolicy拒绝策略

    A. 概述

    CallerRunsPolicy是一种简单的拒绝策略,当任务无法被提交给线程池时,会由提交任务的线程自己执行该任务。

    B. 拒绝策略实现原理

    实现RejectedExecutionHandler接口,在rejectedExecution方法中使用提交任务的线程来执行任务。

    public class CallerRunsPolicy implements RejectedExecutionHandler {
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                r.run();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    C. 应用场景

    适用于对任务提交失败要求较低的场景,通过调用线程来执行任务,避免任务丢失。

    D. 使用示例

    public class Task implements Runnable {
    
        private final int index;
    
        public Task(int index) {
            this.index = index;
        }
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + ":" + index);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    public class Main {
    
        public static void main(String[] args) {
            // 创建线程池
            ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 0L,
                    TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(1),
                    new ThreadPoolExecutor.CallerRunsPolicy());
            try {
                // 提交任务
                threadPool.execute(new Task(1));
                threadPool.execute(new Task(2));
                threadPool.execute(new Task(3));
            } catch (RejectedExecutionException e) {
                e.printStackTrace();
            } finally {
                // 关闭线程池
                threadPool.shutdown();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    四、DiscardPolicy拒绝策略

    A. 概述

    DiscardPolicy是一种简单的拒绝策略,当任务无法被提交给线程池时,会直接丢弃该任务,没有任何提示或处理。

    B. 拒绝策略实现原理

    实现RejectedExecutionHandler接口,在rejectedExecution方法中不做任何操作,即丢弃任务。

    public class DiscardPolicy implements RejectedExecutionHandler {
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            // Do nothing, discard the task
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    C. 应用场景

    适用于对任务提交失败不敏感的场景,对任务丢失没有特殊要求。

    D. 使用示例

    public class Task implements Runnable {
    
        private final int index;
    
        public Task(int index) {
            this.index = index;
        }
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + ":" + index);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    public class Main {
    
        public static void main(String[] args) {
            // 创建线程池
            ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 0L,
                    TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(1),
                    new ThreadPoolExecutor.DiscardOldestPolicy());
            try {
                // 提交任务
                threadPool.execute(new Task(1));
                threadPool.execute(new Task(2));
                threadPool.execute(new Task(3));
            } catch (RejectedExecutionException e) {
                e.printStackTrace();
            } finally {
                // 关闭线程池
                threadPool.shutdown();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    五、DiscardOldestPolicy拒绝策略

    A. 概述

    DiscardOldestPolicy是一种拒绝策略,当任务无法被提交给线程池时,会丢弃最早的一个任务,然后尝试再次提交。

    B. 拒绝策略实现原理

    实现RejectedExecutionHandler接口,在rejectedExecution方法中从队列中获取最早的任务并丢弃,再次提交当前任务。

    public class DiscardOldestPolicy implements RejectedExecutionHandler {
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                e.getQueue().poll();
                e.execute(r);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    C. 应用场景

    适用于对新任务优先级比较高的场景,可以丢弃旧的任务以保证及时处理新任务。

    D. 使用示例

    public class Task implements Runnable {
    
        private final int index;
    
        public Task(int index) {
            this.index = index;
        }
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + ":" + index);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    public class Main {
    
        public static void main(String[] args) {
            // 创建线程池
            ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 0L,
                    TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(2),
                    new ThreadPoolExecutor.DiscardOldestPolicy());
            try {
                // 提交任务
                threadPool.execute(new Task(1));
                threadPool.execute(new Task(2));
                threadPool.execute(new Task(3));
                threadPool.execute(new Task(4));
            } catch (RejectedExecutionException e) {
                e.printStackTrace();
            } finally {
                // 关闭线程池
                threadPool.shutdown();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述

    六、总结

    各种拒绝策略的特点和适用场景

    • AbortPolicy:对任务提交失败要求敏感,需要明确知道任务是否被接受并执行。
    • CallerRunsPolicy:对任务提交失败要求较低,通过调用线程来执行任务,避免任务丢失。
    • DiscardPolicy:对任务提交失败不敏感,对任务丢失没有特殊要求。
    • DiscardOldestPolicy:适用于新任务优先级高,丢弃旧任务以保证及时处理新任务。
  • 相关阅读:
    springboot 2.0 集成 shiro 和 thymeleaf-extras-shiro 2.0 使用过程遇到的问题
    力扣:22-括号生成
    怎么提取视频中的音乐保存到本地?其实方法很简单
    数据结构之循环链表
    这些不知道,别说你熟悉 Spring
    Direct3D颜色
    19.cuBLAS开发指南中文版--cuBLAS中的Level-2函数gemv()
    【opencv-c++】斑点检测即Blob检测
    力扣 第 125 场双周赛 解题报告 | 珂学家 | 树形DP + 组合数学
    Intel汇编-JMP无条件调转
  • 原文地址:https://blog.csdn.net/pengjun_ge/article/details/132767647