2017-04-17 35 views
1

我想向Runnable接口添加異常處理程序。
這裏是我的代碼:如何向執行程序服務添加例外

ExecutorService executor = Executors.newCachedThreadPool(); 

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


} 

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

回答

0

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 ... 
}