抛出RuntimeException的线程-对线程池的影响



我刚刚读了这篇文章,认为如果Runnable抛出异常并退出,线程池可能会耗尽工作线程。我写了一小段代码并检查了一下,但是池大小并没有变化。

public class ThreadPoolTest {
public static void main(String[] args) throws InterruptedException {
    ThreadPoolExecutor tp = new ThreadPoolExecutor(1, 1, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
    Thread.sleep(1000);
    System.out.println(tp.getPoolSize());
    tp.execute(new Runnable(){
        @Override
        public void run() {
            System.out.println("Executing & Throwing");
            throw new NullPointerException();
        }
    });
    Thread.sleep(1000);
    System.out.println(tp.getPoolSize());
    tp.execute(new Runnable(){
        @Override
        public void run() {
            System.out.println("Executing & Throwing");
            throw new NullPointerException();
        }
    });
    Thread.sleep(1000);
    System.out.println(tp.getPoolSize());
    tp.shutdown();
}

}

输出是

0
Exception in thread "pool-1-thread-1" java.lang.NullPointerException
    at threads.ThreadPoolTest$1.run(ThreadPoolTest.java:18)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Executing & Throwing
1
Exception in thread "pool-1-thread-2" java.lang.NullPointerException
    at threads.ThreadPoolTest$2.run(ThreadPoolTest.java:29)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Executing & Throwing
1

这是在Java 7上。是否有任何java线程池实现是脆弱的,如文中所述?

阅读本文,要点是写得不好的线程池可能会出现这个问题。实际上,任何创建线程的应用程序都不能处理来自该线程的未捕获异常。

我希望任何有信誉的线程池都能以某种方式处理这种情况。通过信誉,我将包括oracle来源的解决方案。我怀疑,如果有人是手工编写线程池,而他们缺乏经验,那么他们就会编写一个不那么健壮的解决方案。

文章指出:

标准线程池允许未捕获的任务异常终止池线程

就是这样。一个稍微修改的代码版本显示,当前一个线程由于异常而死亡时,ThreadPool将创建一个新线程。

作为附带注释,您通常会使用submit方法,并尝试在返回的Future上执行get,以查看是否抛出了异常。

输出:

0
Creating new thread
Executing & Throwing
Creating new thread
Exception in thread "Thread-0" java.lang.NullPointerException
    at javaapplication4.Test2$2.run(Test2.java:47)
    ....
1
Executing & Throwing
Creating new thread
Exception in thread "Thread-1" java.lang.NullPointerException
    at javaapplication4.Test2$3.run(Test2.java:56)
    ....
1
代码:

public static void main(String[] args) throws InterruptedException {
    ThreadPoolExecutor tp = new ThreadPoolExecutor(1, 1, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1), new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            System.out.println("Creating new thread");
            return new Thread(r);
        }
    });
    Thread.sleep(1000);
    System.out.println(tp.getPoolSize());
    tp.execute(new Runnable() {
        @Override
        public void run() {
            System.out.println("Executing & Throwing");
            throw new NullPointerException();
        }
    });
    Thread.sleep(100);
    System.out.println(tp.getPoolSize());
    tp.execute(new Runnable() {
        @Override
        public void run() {
            System.out.println("Executing & Throwing");
            throw new NullPointerException();
        }
    });
    Thread.sleep(100);
    System.out.println(tp.getPoolSize());
    tp.shutdown();
}

相关内容

  • 没有找到相关文章

最新更新