如何捕获超时异常

时间:2012-08-06 03:49:34

标签: java multithreading

代码:

Class Manager {
    Future  fu  = pool.invokeAll(workers, SEARCH_TIMEOUT, TimeUnit.SECONDS); 
    // calling the invoke call 
    search search= fu.get();    
   // callable 
}


public class Search implements Callable<Search> {
    Search call() {
        // multiple workers will execute Code So don't want to catch timed out exception in here 
        // api value will be changing based on corresponding reference 
        api.search_api();
    }
}


class api()
{
    search_api(){
        // How to catch a timed out exception in here 
        // catch(TimedoutException){}    did not work in here 
    }
}

有没有办法可以在方法search_api()下的Class api中捕获TimeOut异常?

2 个答案:

答案 0 :(得分:1)

您可以像以下代码一样抓住TimeoutException

try {
        Future fu = pool.invokeAll(workers, SEARCH_TIMEOUT,
                TimeUnit.SECONDS);
        // calling the invoke call
        search search = fu.get();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

您也可以通过以下方式执行此操作

try{

.

.

.

.


catch (RuntimeException e) {
        // TODO: handle exception
}   
相关问题