interrupt方法和事务
来源:5-15 本章常见面试问题

人生苦短,我选Python
2021-01-18
老师,如果有一个子线程run方法是银行执行10笔转账并且是在一个事务里的,转账过程中这个线程被主线程打断也响应了打断并停止了线程,那已转账成功的会回滚吗
4回答
-
业务模型期望的结果是这样的,检测子线程的异常情况,如果发生异常,主线程回滚,否则提交
大家都知道runnable有以下特点:
1.业务处理出现checked exception必须在线程中捕获处理不允许抛出,否则影响run函数的覆盖;
2.如果线程抛出unchecked(runnable) exception,则线程终结,主线程不受影响。
所以使用runnable,主线程压根不知道子线程的情况,事务更无从谈起。
所以使用Callable机制
两种方法
1 使用异常
FutureTask
public V get() throws InterruptedException, ExecutionException
主线程 调用 get 直接抛出异常触发事务管理
2 使用返回
if ("failed".equals(result)) {
throw new RuntimeException();
}
收到failed返回后,抛出异常触发事务管理
012021-01-19 -
悟空
2021-01-19
我已经回答了,稍后会显示
022021-01-19 -
悟空
2021-01-19
run(){}方法中的processEachPlan(learn)不受声明式事务管理了,给你几个参考:
https://www.cnblogs.com/silyvin/p/9106647.html
https://blog.csdn.net/libra_ts/article/details/85165743
可以用Callable记录执行情况并在主线程处理回滚动作
https://blog.csdn.net/silyvin/article/details/79382923?utm_source=blogxgwz3
00 -
悟空
2021-01-19
run(){}方法中的processEachPlan(learn)不受声明式事务管理了,给你几个参考:
https://www.cnblogs.com/silyvin/p/9106647.html
https://blog.csdn.net/libra_ts/article/details/85165743
可以用Callable记录执行情况并在主线程处理回滚动作,例如:
https://blog.csdn.net/silyvin/article/details/79382923?utm_source=blogxgwz3
00
相似问题