如果发生异常,则退出 Java 可调用
Exit from Java Callable if Exception occured
我在 Java 中使用 ExecutorService 和 Callable。 class 实现 Callable 在文件系统上做一些 IO 工作。如果发生异常,如何停止可调用对象的执行并退出?
这是一个 class 实现 Callable 的例子,它有两个方法,foo1() 和 foo2()
public class MyCallable<Object> implements Callable<Object> {
public Object call() throws IOException, SQLException {
// method 1 , could throw IOException
foo1();
// method 2 , could throw SQLException
foo2();
return null;
}
}
这是示例执行服务 class。它可以通过futures对象处理并行处理过程中发生的异常。
public class MyExecutorService {
ExecutorService listProcessor;
listProcessor = Executors.newFixedThreadPool(2);
List<Callable<Object>> callableTodo = new ArrayList<Callable<Object>>();
// add the callables to the todo list
callableTodo.add(new MyCallable<Object>());
callableTodo.add(new MyCallable<Object>());
// start the threads
List<Future<Object>> futures = listProcessor.invokeAll(callableTodo);
listProcessor.shutdown();
listProcessor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
// futures now holds possible exceptions
for (Future<Object> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
// process the exception
}
}
}
但是如果例如在 foo1() 中发生 IOException,我想立即让 MyCallable 停止,而不是让它继续 foo2();
编辑:此外,如果在 foo1() 中发生未经检查的异常(例如 RuntimeException),MyCallable 也需要停止。
Callable<V>
的 call
方法的签名是
V call() throws Exception
它的描述是
Computes a result, or throws an exception if unable to do so.
换句话说,就是不要抓住IOException
。如果你没有捕捉到它,那么执行就会停止并且异常会向上传递一个级别。
注意:如果方法被标记为抛出异常类型,这仅适用于非 RuntimeExceptions,call
被标记为正在执行,因为它被声明为 throws Exception
。
如您所知,如果 Callable
抛出异常,Future
的 .get
方法将抛出 ExecutionException
。
我在 Java 中使用 ExecutorService 和 Callable。 class 实现 Callable 在文件系统上做一些 IO 工作。如果发生异常,如何停止可调用对象的执行并退出?
这是一个 class 实现 Callable 的例子,它有两个方法,foo1() 和 foo2()
public class MyCallable<Object> implements Callable<Object> {
public Object call() throws IOException, SQLException {
// method 1 , could throw IOException
foo1();
// method 2 , could throw SQLException
foo2();
return null;
}
}
这是示例执行服务 class。它可以通过futures对象处理并行处理过程中发生的异常。
public class MyExecutorService {
ExecutorService listProcessor;
listProcessor = Executors.newFixedThreadPool(2);
List<Callable<Object>> callableTodo = new ArrayList<Callable<Object>>();
// add the callables to the todo list
callableTodo.add(new MyCallable<Object>());
callableTodo.add(new MyCallable<Object>());
// start the threads
List<Future<Object>> futures = listProcessor.invokeAll(callableTodo);
listProcessor.shutdown();
listProcessor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
// futures now holds possible exceptions
for (Future<Object> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
// process the exception
}
}
}
但是如果例如在 foo1() 中发生 IOException,我想立即让 MyCallable 停止,而不是让它继续 foo2();
编辑:此外,如果在 foo1() 中发生未经检查的异常(例如 RuntimeException),MyCallable 也需要停止。
Callable<V>
的 call
方法的签名是
V call() throws Exception
它的描述是
Computes a result, or throws an exception if unable to do so.
换句话说,就是不要抓住IOException
。如果你没有捕捉到它,那么执行就会停止并且异常会向上传递一个级别。
注意:如果方法被标记为抛出异常类型,这仅适用于非 RuntimeExceptions,call
被标记为正在执行,因为它被声明为 throws Exception
。
如您所知,如果 Callable
抛出异常,Future
的 .get
方法将抛出 ExecutionException
。