![](/img/trans.png)
[英]Thread doesn't stop on calling stopService() in MainActivity
[英]Calling Thread doesn't stop after RejectedExecutionException
為什么線程池中的某個線程拋出RejectedExecutionException時主線程沒有停止?我在這里做錯了嗎? 線程池中的第三個線程拋出RejectedExecutionException,我沒有在主線程中處理此異常。 因此,我是否一定要處理此異常以使我的主線程停止。 任何幫助,將不勝感激。
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塊?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.