xenoterracide
xenoterracide

Reputation: 16865

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)

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?

Upvotes: 0

Views: 122

Answers (1)

jtahlborn
jtahlborn

Reputation: 53694

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. 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.

Upvotes: 2

Related Questions