-
Couldn't load subscription status.
- Fork 1.7k
Closed
Labels
❓questionFurther information is requestedFurther information is requested📐 design discussion🔰 first nice issue 👍
Description
如果直接用map,在capture 时清理map中的数据,同时在restore 清理map中的数据,会有什么影响?想了半小时,实在没想清楚在什么场景下,下面这代码的意义:
for (Iterator<? extends Map.Entry<TransmittableThreadLocal<?>, ?>> iterator = holder.get().entrySet().iterator();
iterator.hasNext(); ) {
Map.Entry<TransmittableThreadLocal<?>, ?> next = iterator.next();
TransmittableThreadLocal<?> threadLocal = next.getKey();
// backup
backup.put(threadLocal, threadLocal.get());
// clear the TTL values that is not in captured
// avoid the extra TTL values after replay when run task
if (!capturedMap.containsKey(threadLocal)) {
iterator.remove();
threadLocal.superRemove();
}
}同时,在run 的 finally 中restore 的原因也没想明白, 如果线程结束了,那么 把之前的 TransmittableThreadLocal restore回来有什么意义? 线程复用的话,应该是在下次线程执行时复用 启动线程的 TransmittableThreadLocal,而不是restore 的TransmittableThreadLocal。
谢谢啦解答下。
Metadata
Metadata
Assignees
Labels
❓questionFurther information is requestedFurther information is requested📐 design discussion🔰 first nice issue 👍
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
oldratlee commentedon Aug 3, 2019
提供反例式的回答
你说的是 map 是指
holder吗?如果是,说明如下:『在capture 时清理map中的数据』 的问题
『在restore 清理map中的数据』 的问题
提交到线程池的任务 可能 在本线程直接执行(参见 『
CallerRunsPolicy』。问题说明如下:
还是 因为 『提交到线程池的任务 可能 在本线程直接执行』『Restore』确保没有上面的
Bug。按原则的回答
原则:通过 整体流程/设计/代码实现 来 分析/证明 正确性。 @soca2013
CRR(Capture/Replay/Restore)是一个面向上下文传递设计的流程,通过这个流程的分析可以保证/证明 正确性。这个正确性的分析/证明 ,不依赖于 局部与反例。
总结一下:尽量首先去确定分析自己程序的正确性,而不是找反例。不分析而去依赖反例,又因为经验受限找不到反例认为没问题而上线,这其实就是我们程序出bug的原因。
@soca2013 有说得不明白的地方,欢迎交流。 ❤️
PS
如果你有兴趣『整体流程与分析』推荐:
TransmittableThreadLocal的系统流程、查看代码实现。