Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ThreadLocal variables

 
Mohan Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API can tell you. What don't you understand about what it says?
 
Mohan Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohan Sharma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic