File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread Local Variables - Obtaining the ThreadLocal object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Local Variables - Obtaining the ThreadLocal object" Watch "Thread Local Variables - Obtaining the ThreadLocal object" New topic
Author

Thread Local Variables - Obtaining the ThreadLocal object

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1024
    
  15
Hello, All.

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.

Chan.

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.

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1024
    
  15
Ok, so I've found this very helpful thread in coderanch.

http://www.coderanch.com/t/520115/threads/java/ThreadLocal-variable-static-TL-variable#2355661

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.

Henry


So does this mean the values are stored in each thread that registers itself with the ThreadLocal object? So does that mean this...

There is a ThreadLocal variable, say tl.

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.

Chan.

Maxim Karvonen
Ranch Hand

Joined: Jun 14, 2013
Posts: 103
    
  11
Not sure if I have understood it right.


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.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1024
    
  15
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.

Thank you.
Chan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Local Variables - Obtaining the ThreadLocal object