So, I'm back again with another Thread concept that I am trying to understand currently and I'm hoping you guys would help me.
I understand that every thread accessing a ThreadLocal variable gets its own copy of the variable. But when a thread accessing it invokes a get() method on the variable, is it a synchronized call on all other threads? Actually my question is a result my own ( hence it can be wrong ) interpretation of what I'm currently reading ( 'Java Threads', third edition, Chapter 5- Minimal Synchronization Techniques - Page 108, 3rd para ).
To elaborate more on that, consider we have a class such as
The authors state that the above mentioned approach to have ThreadLocal objects is worth considering if the processing done in someMethod is very expensive.
This is because obtaining the <ThreadLocal object> itself requires synchronizing on all the threads.
The part in bold is what I have not understood. Could somebody please explain it in a little more detail. Thanks so much for that.
Request the moderators to apply corrective actions on the question if they think it's violating copyrights of the book. That wasn't my intent. I have tried to keep only a small portion ( just relevant for the question ) of the content in my question and I have tweaked that a great deal too.
Joined: Sep 06, 2012
Ok, so I've found this very helpful thread in coderanch.
In the mentioned thread, Henry states the following.
Actually, that is not how threadlocals work -- it is just easier to think of it as a map that routes values based on the thread id as the key.... meaning you can envision it that way. It is not a disguised map.
As already mentioned, the ThreadLocal class actually stores the values internally in the Thread object that represents the thread. The data is actually not stored in the ThreadLocal object.
So does this mean the values are stored in each thread that registers itself with the ThreadLocal object? So does that mean this...
Thread1 registers itself with it. The fact that Thread1 is registered with tl is stored through some internal mechanism in Thread1.
Thread2 registers itself with tl. Now the fact that Thread1 and Thread2 are registered with tl is stored in Thread1 and Thread2. would that be correct?
Not sure if I have understood it right. But if I have, this sort of explains why the calls are synchronized. I'm sure you'd correct me ( and it would be a great help to me if you choose to do so ), if I am wrong.
I think you understood it correct. But it does not require any synchronization. In your explanation tl is registered in both threads. This operation requires access to a corresponding thread's storage. But that thread's storage is always accessed by only that thread (there are no API to access threadLocal value for other thread). And ThreadLocal does not modify itself any way in this registartion. You cannot tell in which threads ThreadLocal was registered if you have only a tl instance.
Recent implementations of JVM (I checked Oracle JDK 7) don't use any synchronization at all. You may look into a ThreadLocal's source, you can find it in src.zip in your JDK folder.
I don't know, why author of the book talks about global synchronization. Maybe ThreadLocals was implemented that way in some earlier versions of JVM.
Joined: Sep 06, 2012
Thanks so much, Maxim.
Yeah the book ( very nice book- I love this book ) was written way back in 2004.
I just checked the src zipped folder. Wow. Didn't know we had the source files available to us. I thought all I would get is class files. Good, now I can peek into this folder every once in a while.
subject: Thread Local Variables - Obtaining the ThreadLocal object