簡體   English   中英

如何向執行程序服務添加例外

[英]How to add exceptions to executor service

我試圖將異常處理程序添加到Runnable接口。
這是我的代碼:

 ExecutorService executor = Executors.newCachedThreadPool();

public ResponseEntity<String> handleNotifications(){
   Runnable r1 =() ->{
             //some code
};
executor.execute(r1);


}

如何將異常處理程序添加到runnable接口。

Runnable接口添加(已檢查)異常處理程序幾乎等同於Callable<T>接口。 我建議調查一下以解決您的問題,否則您只需使用try-catch塊。

ExecutorService executor = Executors.newCachedThreadPool();

public ResponseEntity<String> handleNotifications(){
    executor.execute(() -> {
        try {
            // Code here...
        } catch (Exception e) {
            // Handle exception here...
        }
    });

    return ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM