• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is ThreadLocal

 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some one pointed me that this a helpful class.

In what context can I use this class???
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quoting Javadoc API:

This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable.


So it represents the support for storing thread variables.

./pope
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object stored in the ThreadLocal is not shared between threads. I think the web container normally stores the user/security related information in a ThreadLocal. I guess methods like getUserPrincipal(), isUserinRole() etc provided by a container get the information from the thread local variables for that particular request/thread.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, in all my years of development, I have rarely found a case where thread local variables were useful... ... This is not to say it is useless, just that I don't use it much.

Basically, this class should be used when you need variables in your methods that is unique per thread. You can accomplish the same functionality by storing your local variables into a hashtable using the current thread as the key. (In fact, internally, this may be how it is done)

The advantage with thread local variables is that there is no need to synchronize.

Henry
 
Victor Ho
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found ThreadLocal to be quite useful in system logging.

During a thread of execution, the thread could be running in different classes/methods in its lifetime. If we would like the log of this thread of execution to always print out certain data (other than the thread id) for the ease of tracking the execution flow, we could make use of ThreadLocal to store such data so that it is available for logging no matter which class/method it is in (which might not be available otherwise).
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
article on ThreadLocal may help
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops the sample chapter from the book has a reference to ThreadLocals!
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
To be honest, in all my years of development, I have rarely found a case where thread local variables were useful... ... This is not to say it is useless, just that I don't use it much.
Henry


One of the most interesting usages I have found for ThreadLocal wis for the management of Hibernate Session in session beans.

./pope
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Victor Ho:
I have found ThreadLocal to be quite useful in system logging.

During a thread of execution, the thread could be running in different classes/methods in its lifetime. If we would like the log of this thread of execution to always print out certain data (other than the thread id) for the ease of tracking the execution flow, we could make use of ThreadLocal to store such data so that it is available for logging no matter which class/method it is in (which might not be available otherwise).


This sounds interesting. But how you store data in those ThreadLocal?

./pope
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!