aspose file tools
The moose likes Threads and Synchronization and the fly likes Using ThreadLocal with static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Using ThreadLocal with static methods" Watch "Using ThreadLocal with static methods" New topic
Author

Using ThreadLocal with static methods

Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 279
Is it possible to use ThreadLocal with static methods? I have a utility class with static methods. On rare occasions, it would be desirable to retain state for a thread (user). I have examined the ThreadLocal class code and, given the way it constructs a key in its constructor, it does not appear to be possible using the ThreadLocal class itself.

Creating separate instances of this class is not an option. I would rather forgo the retention of state rather than give up the convenience that static methods currently provide.

I am not opposed to maintaining a Map of all threads and their corresponding state. However, if I choose this approach, is there a way to know when to remove an entry from the Map, e.g. the thread terminates or returns to a pool?

Any suggestions would be appreciated.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

I'm not sure I understand the issue. The class with the static methods should hold an instance of ThreadLocal in a static variable.


[Jess in Action][AskingGoodQuestions]
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 279
I was concerned that, if I maintained a ThreadLocal variable as a static variable, it would not be able to distinguish between different threads (users). However, upon reviewing the ThreadLocal code again, I see I glossed over the fact that ThreadLocal always retrieves the thread local values for the current thread so it appears my concern may be moot. Is this correct?

My other concern was that, if the thread originates from a thread pool the thread local values for that thread be reset (initialized) each time it is returned from the pool. I would not want thread local values to be retained for subsequent users of the thread. Is it reasonable to assume that the thread will be re-initialized each time it is returned from a pool?
Sverre Moe
Ranch Hand

Joined: Jul 10, 2007
Posts: 109
I myself use ThreadLocal with static methods of a utility object and it works fine.
[ July 27, 2007: Message edited by: Sverre Moe ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

Basically, a ThreadLocal is a Map. The keys are Thread objects, and the Values are whatever you store in the ThreadLocal. When you ask the ThreadLocal for its value, it looks up the value in the Map, using Thread.currentThread() as the key. That's all -- nothing you couldn't code easily yourself. For some reason a lot of people think this class is mysteriously powerful, but it's really quite trivial. So for your first question, the answer is yes, that's correct.

For the second question, if the value must be reset when a Thread is taken from a pool, then the Thread is going to have to do that itself; it will not happen automatically. The ThreadLocal class knows nothing about your pool.
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 279
Thanks all.

Count me among those who thought this class "mysteriously powerful". I should have read the Javadoc more closely. I have since confirmed (as you already know) that it works the way I want it do.
[ July 27, 2007: Message edited by: Jay Damon ]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Using ThreadLocal with static methods
 
Similar Threads
ThreadLocal Type Safety "Generics"
thrading + class instance
Some ThreadLocal issues
ThreadLocale Variable
How to use ThreadLocal class