aspose file tools*
The moose likes Threads and Synchronization and the fly likes ThreadLocal variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ThreadLocal variables " Watch "ThreadLocal variables " New topic
Author

ThreadLocal variables

Mohan Sharma
Greenhorn

Joined: Nov 13, 2006
Posts: 10
Hi All ,

Can anyone please explain the purpose of ThreadLocal variables.What is the purpose of using them ?

Thanks in advance.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
The API can tell you. What don't you understand about what it says?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Mohan Sharma
Greenhorn

Joined: Nov 13, 2006
Posts: 10
Hi Peter,

Actually in my application there is a Logging Interceptor (working with JDK dynamic proxy).I want to create a single instance of this Logging Interceptor for whole application.




LoggingInterceptor


Configurer class maintains a hashmap which is initialized at class load and returns the same instance of LoggingConfigurer every time.

If multiple threads are accessing the same instance of LoggingInterceptor then there can be a situation where a thread-1 set the logger in "before" method of interceptor and thread-2 changes it when it access the "before" method before completion of "after" method of first thread .One solution for this problem is using synchronized block ,but can we use ThreadLocal variables here .

I guess problem is clear to you ,please ask if I am not clear in stating the problem.
Mohan Sharma
Greenhorn

Joined: Nov 13, 2006
Posts: 10
Configurer class maintains a hashmap which is initialized at class load and returns the same instance of LoggingConfigurer every time.


It is LoggingInterceptor .
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I don't quite get what you want to achieve and why ThreadLocal might be used.

If you want a single instance per application, you usually want some sort of Singleton pattern. This can be implemented with synchronisation, or better with the "singleton holder" idiom (Google it). Note that you can only enforce a single instance per ClassLoader, not per JVM. If you have complex class-loading, think carefully if you're getting something good enough.

If you want a different instance per thread, ThreadLocal is a good way to achieve it. But that's not what you want, is it?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you have a single instance of LoggingInterceptor, a ThreadLocal to hold your Logger should be fine:

What does Logger.getLogger(clazz) return ... a new instance of Logger every time or something from a cache? If logger could potentially be shared, this ThreadLocal business won't help much.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mohan Sharma
Greenhorn

Joined: Nov 13, 2006
Posts: 10
Hi Stan ,

Thanks for the reply.

Yes ,there is a single instance of LoggingInterceptor but the loggers are not shared .Each time LoggingInterceptor is accessed by a thread a new logger instace is assinged to instance variable "logger" of class LoggingIntercetor.Now What I want to ask is that if I use

private ThreadLocal<Logger> threadLocalLogger ;

in place of

private Logger logger ;

will I face the problem where thread-1 accessing instance of LoggingInterceptor runs "before" method in which it create a logger for a particular class and sets it in ThreadLocal class variable "threadLocalLogger" now before the completion of "after" method of thread 1 , thread-2 comes in access the same instance of LoggingInterceptor sets its own logger in threadLocalLogger and runs "before" method .Now if thread-1 access the threadLocalLogger in "after" method which instance of logger will it get the one set by himself or the one set by thread-2 .

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ThreadLocal variables
 
Similar Threads
display data for a thread of many threads
Local Variable
Thread Safety in a Single Instance of a class
Is the following Class Thread Safe ?.
Some ThreadLocal issues