调用线程在拒绝执行异常后不会停止



为什么当线程池中的一个线程抛出拒绝执行异常时,主线程没有停止?我在这里做错了什么吗?线程池中的第三个线程抛出拒绝执行异常,我没有在主线程中处理此异常。所以我必须处理这个异常以使我的主线程停止。任何帮助将不胜感激。

public static void main(String[] args) {
ExecutorService threadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(1));
threadPool.execute(new TestOne());
threadPool.execute(new TestTwo());
threadPool.execute(new TestThree());
threadPool.shutdown();
try {
threadPool.awaitTermination(2L, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("main thread stopped");
}
}
class TestOne implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Executing task one");
try {
Thread.sleep(10);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
class TestTwo implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Executing task two");
try {
Thread.sleep(10);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
class TestThree implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("Executing task three");
try {
Thread.sleep(10);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}

线程池中没有线程引发RejectedExecutionException。将任务提交到线程池的主线程实际上是抛出RejectedExecutionException

主线程不会在提交时阻塞,因为规范规定它不应该阻塞。有一些机制可以做到这一点,请参阅 ThreadPoolExecutor block当队列已满?

最新更新