美文网首页
AbortPolicy

AbortPolicy

作者: bigdata张凯翔 | 来源:发表于2021-04-15 22:43 被阅读0次

    AbortPolicy(默认):直接抛出RejectedExecutionException异常阻止系统正常运行。

    package com.istudy;
    
    import java.util.concurrent.*;
    
    /**
     * @projectname: HelloFlink
     * @description:AbortPolicy
     * @author: Mr.Zhang
     * @create: 2021-04-15 22:39
     **/
    public class ThreadPoolTest {
        /**
         *
         * @param args
         */
        public static void main(String[] args) {
            //这里是一个默认的线程池,没有设置拒绝策略,设置了最大线程队列是100。
            //线程池的默认拒绝策略为AbortPolicy,即丢弃任务并抛出RejectedExecutionException异常
            BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(100);
            ThreadFactory factory = r -> new Thread(r, "test-thread-pool");
            ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5,
                    0L, TimeUnit.SECONDS, queue, factory);
            while (true) {
                executor.submit(() -> {
                    try {
                        System.out.println(queue.size());
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:AbortPolicy

          本文链接:https://www.haomeiwen.com/subject/futslltx.html