Do operations on ThreadLocal have to be synchronized?
|Added at||2016-12-25 19:12|
Here is the code I've stumbled across:
The currentTransactionContext field is of type ThreadLocal and it is the only field in the class.
It seems to me that synchronization is not needed here because value stored in ThreadLocal is associated with particular thread and thus it's not a shared state. In addition I think it impacts performance as currentTransactionContext itself is shared and only one thread is allowed to enter the block while many can do it in parallel without impacting correctness.
Is the synchronization needed here?