美文网首页
Java线程池的四种拒绝策略

Java线程池的四种拒绝策略

作者: herohua | 来源:发表于2020-02-12 12:56 被阅读0次

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

相关文章

  • Java线程池拒绝策略

    【Java线程池拒绝任务策略】创建线程池可以指定拒绝策略如下: 一 拒绝时机1) 调用线程池的shutdown函数...

  • 多线程juc线程池

    java_basic juc线程池 创建线程池 handler是线程池拒绝策略 排队策略 线程池状态 RUNNIN...

  • ThreadPoolExecutor的RejectedExecu

    java 线程池ThreadPoolExecutor的拒绝策略有: CallerRunsPolicy : 当线程池...

  • 线程池

    Java中的多线程了解么,线程池的增长策略和拒绝策略了解么java.util.concurrent.ThreadP...

  • 线程池终探

    线程池四种拒绝策略 AbortPolicy 直接抛异常 DiscardPolicy 丢弃不处理 DiscardO...

  • ThreadPoolExecutor

    线程池 拒绝策略 线程池的拒绝策略,即任务被添加到线程池中被拒绝而采取的处理措施。任务被拒绝的原因可能有: 线程池...

  • 线程池拒绝策略

    jdk默认提供以下四种线程池拒绝策略 AbortPolicy:直接抛出一个RejectedExecutionExc...

  • java之常用四种线程

    Java四种线程池的使用 Java通过Executors提供四种线程池,分别为: newCachedThreadP...

  • Java 线程池四种拒绝策略

    jdk1.5版本新增了 JUC 并发包,其中一个包含线程池。 四种拒绝策略: 拒绝策略类型说明1ThreadPoo...

  • 面试官:说说你知道多少种线程池拒绝策略

    前言 线程池,相信很多人都有用过,没用过相信的也有学习过。但是,线程池的拒绝策略,相信知道的人会少许多。 四种线程...

网友评论

      本文标题:Java线程池的四种拒绝策略

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