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: 拒絕任務(wù)

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:直接使用調(diào)用線程執(zhí)行任務(wù)

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:拋棄隊列中的未執(zhí)行的任務(wù),嘗試重新執(zhí)行

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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容