AbortPolicy

2021-04-15  本文已影响0人  bigdata张凯翔

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();
                }
            });
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读