Java线程池的四种拒绝策略

1.AbortPolicy:抛出异常

throws a {@code RejectedExecutionException}.

private static void testAbortPolicy() throws InterruptedException {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(1),
            r -> {
                Thread thread = new Thread(r);
                return thread;
            }, new ThreadPoolExecutor.AbortPolicy());

    for (int i = 0; i < 3; i++) {
        executor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    TimeUnit.SECONDS.sleep(1);

    executor.execute(() -> {
        System.out.println("Can this task execute???");
    });
}
拒绝策略1.png

2.DiscardPolicy: 拒绝任务

silently discards the rejected task.

private static void testDiscardPolicy() throws InterruptedException {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(1),
            r -> {
                Thread thread = new Thread(r);
                return thread;
            }, new ThreadPoolExecutor.DiscardPolicy());

    for (int i = 0; i < 3; i++) {
        executor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    TimeUnit.SECONDS.sleep(1);

    executor.execute(() -> {
        System.out.println("Can this task execute???");
    });
}
拒绝策略2ng

3.CallerRunsPolicy:直接使用调用线程执行任务

runs the rejected task directly in the calling thread of the {@code execute} method,unless the executor has been shut down, in which case the task is discarded.

private static void testCallerRunsPolicy() throws InterruptedException {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(1),
            r -> {
                Thread thread = new Thread(r);
                return thread;
            }, new ThreadPoolExecutor.CallerRunsPolicy());

    for (int i = 0; i < 3; i++) {
        executor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    TimeUnit.SECONDS.sleep(1);

    executor.execute(() -> {
        System.out.println("Can this task execute??? Yes, is execute in thread " +  Thread.currentThread().getName());
    });
}
拒绝策略3.png

4.DiscardOldestPolicy:抛弃队列中的未执行的任务,尝试重新执行

discards the oldest unhandled request and then retries {@code execute},unless the executor is shut down, in which case the task is discarded.

private static void testDiscardOldestPolicy() throws InterruptedException {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 2, 30, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(1),
            r -> {
                Thread thread = new Thread(r);
                return thread;
            }, new ThreadPoolExecutor.DiscardOldestPolicy());

    for (int i = 0; i < 3; i++) {
        executor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
                System.out.println("I am from lambda.");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    TimeUnit.SECONDS.sleep(1);

    executor.execute(() -> {
        System.out.println("Can this task execute??? Yes, is execute in thread " +  Thread.currentThread().getName());
    });
}
拒绝策略4.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容