關閉時可使用如下代碼
public static void waitUntilTerminate(final ExecutorService executorService, final int timeout) {
try {
executorService.shutdown();
if (!executorService.awaitTermination(timeout, TimeUnit.SECONDS)) { //超時后直接關閉
executorService.shutdownNow();
}
} catch (InterruptedException e) { //awaitTermination 出現中斷異常也將觸發關閉
executorService.shutdownNow();
}
}
但是實際使用中,可能會出現即使使用了shutdownNow
方法,還是無法終止線程的問題,那是因為你的線程無法被中斷
shutdownNow
方法簡單理解就是給在運行的線程發一個中斷信號,如果你的線程忽略這個信號,那就無法停下來
舉個例子來說明這個問題
public class ShutDownUtilsTest {
private ExecutorService executorService;
@Before
public void init() {
executorService = Executors.newFixedThreadPool(1);
}
@Test
public void shutDownOKTest() {
ShutDownUtils.waitUntilTerminate(executorService, 1);
CommonUtils.sleep(1); //等待線程處理中斷
Assert.assertTrue(executorService.isTerminated());
}
@Test
public void shutDownNowFailTest() {
executorService.execute(this::canNotStopThread);
ShutDownUtils.waitUntilTerminate(executorService, 0);
CommonUtils.sleep(1); //等待線程處理中斷
Assert.assertFalse(executorService.isTerminated());
}
@Test
public void shutDownNowOKTest() {
executorService.execute(this::stopThread);
ShutDownUtils.waitUntilTerminate(executorService, 0);
CommonUtils.sleep(1); //等待線程處理中斷
Assert.assertTrue(executorService.isTerminated());
}
private void canNotStopThread() {
for (long i = 0; i < Long.MAX_VALUE; i++) {
}
}
private void stopThread() {
for (long i = 0; i < Long.MAX_VALUE && !Thread.currentThread().isInterrupted(); i++) {
}
}
}
從上面的測試用例可以看到canNotStopThread
無法被shutDownNow
終止
然而stopThread
可以被正常終止,因為通過Thread.currentThread().isInterrupted()
在判斷線程是否收到了中斷信號