简体   繁体   English

如果我的值存储在“线程安全”映射中,那么该值是否需要为ThreadLocal?

[英]If my value is stored in a Thread Safe map does the value need to be ThreadLocal?

I'm storing objects in a ThreadLocal Map ( implemented in spring-tx) 我将对象存储在ThreadLocal Map (在spring-tx中实现)

class TransactionSynchronizationManager ...

private static final ThreadLocal<Map<Object, Object>> resources =
        new NamedThreadLocal<Map<Object, Object>>("Transactional resources");

if the Map is ThreadLocal and properly accessed (again all spring-tx code), do the values I store in the map have to be ThreadLocal to be thread safe? 如果Map是ThreadLocal并且可以正确访问(再次使用所有spring-tx代码),那么我存储在映射中的值是否必须是ThreadLocal才是线程安全的?

Assuming you haven't inadvertently passed the Map to another thread by some other means, the Map does not need any additional protection, because all usage of it will be single-threaded. 假设您没有通过其他某种方式无意中将Map传递给另一个线程,则Map不需要任何额外的保护,因为它的所有用法都是单线程的。 Likewise, as long as any Objects in the Map are not inadvertently passed to other threads by other means, those Objects do not need any additional protection. 同样,只要Map中的任何Object均不会通过其他方式无意传递给其他线程,则这些Object不需要任何其他保护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM