Java线程池的四种拒绝策略

2020-02-12  本文已影响0人  herohua

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
上一篇 下一篇

猜你喜欢

热点阅读