This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes When should i use ThreadLocal? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "When should i use ThreadLocal?" Watch "When should i use ThreadLocal?" New topic
Author

When should i use ThreadLocal?

rup gupta
Greenhorn

Joined: Apr 26, 2011
Posts: 6
When should i use ThreadLocal? What would I acheive from that? Is there any scenerios where I should use ThreadLocal?
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

Without using ThreadLocal, each instances of a class is shared among all the threads being provisioned and can easily create some thread issues if that class is not designed as thread safe.

With using ThreadLocal, each thread will create its own instance of that class and isolate that instance from other threads.

When to use:
It is a great utility to use when trying to solve thread issues.
My 2 cents.


SCJP, SCJD, SCWCD, SCBCD, SCEA
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35439
    
    9
Perhaps a better introduction: http://www.ibm.com/developerworks/java/library/j-threads3/index.html


Android appsImageJ pluginsJava web charts
sean lopez
Greenhorn

Joined: Oct 31, 2008
Posts: 25
I hope this will help you
http://www.javamex.com/tutorials/synchronization_concurrency_thread_local3.shtml


Test Management
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: When should i use ThreadLocal?
 
Similar Threads
ThreadLocal.initialValue() used for lazy loading ?
Advantage of Threadlocal over session object
Regarding ThreadLocal and Volatile
java.net.SocketException: Broken pipe
when are static ThreadLocal variables intialized