【发布时间】:2013-09-11 19:04:07
【问题描述】:
我正在使用 ThreadPool 执行器,并使用 invokeAll() 方法调用任务。在可调用的 call() 方法中,我写入数据库,但如果我确定线程执行良好并且没有严重终止,我只想写入。
这些解决方案是否有效:
解决方案 1
我认为这不起作用,因为线程可以在数据库写入后被中断,所以它不会返回结果,我猜。
public Result call() throws exception(){
...
if( !Thread.currentThread.isInterrupted() ){
//Save to the database
}
...
return result;
}
解决方案 2
在 get() 方法之后处理写入以确保它正常终止,如果我理解正确,get() 方法会重新抛出执行期间捕获的任何异常
...
for( Future f : futures){
try {
Result r = f.get();
//Do the write with the result i got
}
catch( Exception e){
//Something went wrong
}
}
提前致谢。
【问题讨论】:
标签: java android exception threadpool callable