• 线程池(4)-参数-RejectedExecutionHandler


    1.介绍

    当线程池线程数大于最大线程数(maximumPoolSize)时,多余的任务,程序应该按照什么拒绝策略处理。

    2.拒绝策略4个

    AbortPolicy:丢弃任务,并抛出RejectedExecutionException异常(需要在调用线程处捕获异常,即执行submit线程处)

    DiscardPolicy:丢弃任务,不抛出异常

    DiscardOldestPolicy:丢弃等待队列最前面任务

    CallerRunsPolicy:由调用线程处理该任务

    3.示例

    3.1.AbortPolicy捕获异常

    public class RejectedAbortPolicy {
    
        static class MyRunnable implements Runnable {
            private String jobName;
    
            MyRunnable(String jobName) {
                this.jobName = jobName;
            }
    
            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.currentThread();
                        Thread.sleep(1000);
                        System.err.println("当前线程:" + Thread.currentThread().getName() + " 当前任务:" + jobName);
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            ExecutorService es = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1),
                    Executors.defaultThreadFactory(), new AbortPolicy());
            es.submit(new MyRunnable("job-1"));
            es.submit(new MyRunnable("job-2"));
            es.submit(new MyRunnable("job-3"));
            Thread.sleep(5000);
            try {
                Future<?> f = es.submit(new MyRunnable("job-4"));
                f.get();// 这一句永远执行不到
            } catch (Exception e) {
                System.err.println("job-4 submit Thread pool mistake");
            }
        }
    }

    3.2.DiscardOldestPolicy测试丢弃等待队列最前面任务

    public class RejectedDiscardOldestPolicy {
        static class MyRunnable implements Runnable {
            private String jobName;
    
            MyRunnable(String jobName) {
                this.jobName = jobName;
            }
    
            public String getJobName(){
                return jobName;
            }
            
            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.currentThread();
                        Thread.sleep(1000);
                        System.err.println("当前线程:" + Thread.currentThread().getName() + " 当前任务:" + jobName);
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(1);
            ExecutorService es = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, workQueue,
                    Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
            es.execute(new MyRunnable("job-1"));
            es.execute(new MyRunnable("job-2"));
            es.execute(new MyRunnable("job-3"));
            Runnable firstRunnable = workQueue.poll();
            System.err.println("submit job-3 检查队列当前 Runnable名称:" +((MyRunnable) firstRunnable).getJobName());
            workQueue.add(firstRunnable);
            
            Thread.sleep(5000);
            es.execute(new MyRunnable("job-4"));
            Runnable SecondRunnable = workQueue.poll();
            System.err.println("submit job-4 检查队列当前 Runnable名称应该为job-4,实际为:" +((MyRunnable) SecondRunnable).getJobName());
            workQueue.add(SecondRunnable);
        }
    }

    3.3.CallerRunsPolicy主线程处理这个任务

    public class RejectedCallerRunsPolicy {
        static class MyRunnable implements Runnable {
            private String jobName;
    
            MyRunnable(String jobName) {
                this.jobName = jobName;
            }
    
            public String getJobName(){
                return jobName;
            }
            
            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.currentThread();
                        Thread.sleep(1000);
                        System.err.println("当前线程:" + Thread.currentThread().getName() + " 当前任务:" + jobName);
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(1);
            ExecutorService es = new ThreadPoolExecutor(2, 2, 0, TimeUnit.SECONDS, workQueue,
                    Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
            es.execute(new MyRunnable("job-1"));
            es.execute(new MyRunnable("job-2"));
            es.execute(new MyRunnable("job-3"));
            
            Thread.sleep(5000);
            es.execute(new MyRunnable("job-4"));
        }
    }
  • 相关阅读:
    ubuntu server编译安装nginx
    XPath具体解释
    windows下安装,配置gcc编译器
    给字符数组赋值的方法
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    MiinCMP1.0 SAE 新浪云版公布, 开源企业站点系统
    Mac下cocos2dx-3.0打包Android时,提示&quot;SimpleAudioEngine.h&quot;not found的解决方法
    GG同步到sqlserver报错一例 Invalid date format
    分布式文件系统
    动画clip仅仅读的解决的方法,以及动画关键帧回调的办法
  • 原文地址:https://www.cnblogs.com/SmilingEye/p/11752844.html
Copyright © 2020-2023  润新知