この記事を読んだところ、 が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 Threadpool 実装はありますか?