Java利用異常中斷當前任務的技巧分享
Java小技巧:利用異常中斷當前任務
在日常開發(fā)中,我們經(jīng)常遇到調(diào)用別人的代碼來完成某個任務,但是當代碼比較耗時的時候,沒法從外部終止該任務。有些程序從開始就考慮到了這個場景,就會提供對應的cancel
或stop
之類的方法用于終止任務,但還是會有很多三方庫并沒有提供響應接口。比如,下面這個下載示例:
class DownloadTask { public void download(Listener listener) { try { for (int i = 0; i < 100; i++) {//模擬下載過程 Thread.sleep(1000); listener.onProgressUpdated(i + 1); } listener.onCompleted(); } catch (InterruptedException e) { throw new RuntimeException(e); } } public interface Listener { void onProgressUpdated(int progress); void onCompleted(); } } public class Main { public static void main(String[] args) throws InterruptedException { DownloadTask task = new DownloadTask(); task.download(new DownloadTask.Listener() { @Override public void onProgressUpdated(int progress) { System.out.println("onProgressUpdated: " + progress); if (isInterrupted.get()) { throw new EndException(); } } @Override public void onCompleted() { } }); } }
這里的DownloadTask
開始運行后,就無法直接停止。如果DownloadTask
是第三方庫的代碼時,我們可能不方便改它的代碼使其支持中斷。本文介紹一種利用異常來終止這樣的任務的方法。
(注:本文介紹的方法僅限于單線程或單個子線程的情況,并且需要有在任務運行的線程執(zhí)行代碼的能力,比如回調(diào))
場景1:任務沒有另起線程的情況
這個就是上面展示的那個下載示例,我們可以通過回調(diào),直接利用異常中斷該任務:
//DownloadTask 同上 public class Main { private static volatile boolean isInterrupted; public static void main(String[] args) throws InterruptedException { new Thread(() -> {//模擬外部調(diào)用,5秒后通過設置isInterrupted來取消任務,也可以把這段代碼放到一個新的cancel方法中 sleep(5000); isInterrupted = true; }).start(); try { DownloadTask task = new DownloadTask(); task.download(new DownloadTask.Listener() { @Override public void onProgressUpdated(int progress) { System.out.println("onProgressUpdated: " + progress); if (isInterrupted) { throw new EndException(); } } @Override public void onCompleted() { System.out.println("onCompleted..."); } }); }catch (EndException e){ System.out.println("end..."); //is interrupted, ignore } Thread.currentThread().join(); } static class EndException extends RuntimeException { } public static void sleep(long time) { try { Thread.sleep(time); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
場景2:任務另起子線程的情況
如果任務另起子線程來執(zhí)行,那么就沒法直接用try-catch
來捕獲異常,那么就得通過線程的UncaughtExceptionHandler
來完成。假如下載的代碼是這樣的:
public class DownloadTask { public void download(Listener listener) { new Thread(() -> { try { for (int i = 0; i < 100; i++) {//模擬下載過程 Thread.sleep(1000); listener.onProgressUpdated(i + 1); } listener.onCompleted(); } catch (InterruptedException e) { throw new RuntimeException(e); } }).start(); } public interface Listener { void onProgressUpdated(int progress); void onCompleted(); } }
那么外部調(diào)用并終止的代碼就是:
public class Main { private static volatile boolean isInterrupted; public static void main(String[] args) throws InterruptedException { Thread.UncaughtExceptionHandler handler = (t, e) -> { if(e instanceof EndException){ System.out.println("end..."); //is interrupted, ignore } }; DownloadTask task = new DownloadTask(); task.download(new DownloadTask.Listener() { @Override public void onProgressUpdated(int progress) { System.out.println("onProgressUpdated: " + progress); if(Thread.currentThread().getUncaughtExceptionHandler() != handler){//注意這里 Thread.currentThread().setUncaughtExceptionHandler(handler); } if (isInterrupted) { throw new EndException(); } } @Override public void onCompleted() { System.out.println("onCompleted..."); } }); sleep(5000); isInterrupted = true; Thread.currentThread().join(); } public static class EndException extends RuntimeException { } public static void sleep(long time) { try { Thread.sleep(time); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
寫在最后
大家也能看到,這并不是什么萬能的方法,甚至有很大局限,但碰到合適的場景還是可以通過簡短的代碼來解決問題。
到此這篇關于Java利用異常中斷當前任務的技巧分享的文章就介紹到這了,更多相關Java中斷任務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis plus開發(fā)過程中遇到的問題記錄及解決
這篇文章主要介紹了mybatis plus開發(fā)過程中遇到的問題記錄及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot Event實現(xiàn)異步消費機制的示例代碼
這篇文章主要介紹了SpringBoot Event實現(xiàn)異步消費機制,ApplicationEvent以及Listener是Spring為我們提供的一個事件監(jiān)聽、訂閱的實現(xiàn),內(nèi)部實現(xiàn)原理是觀察者設計模式,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下2024-04-04