方式一: 使用Callable, 利用Callable的返回值判断是否需要进行事务回滚
ExecutorService service = Executors.newCachedThreadPool();
Future<Integer> submit = service.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
System.out.println("bla bla ...");
return 5 * 3;
}
});
try {
if (submit.get() == 15) {
throw new RunTimeException("操作失败!");
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
方式二: 使用FutureTask
Callable<Integer> integerCallable = new Callable<Integer>() {
@Override