aspose file tools*
The moose likes Threads and Synchronization and the fly likes What is thread-local variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "What is thread-local variable" Watch "What is thread-local variable" New topic
Author

What is thread-local variable

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
I read one statement from http://download.oracle.com/javase/1.4.2/docs/api/java/lang/ThreadLocal.html

that Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible.
What does it mean?


I am slow but sure
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40


The ThreadLocal class provide copies of a variable for each thread. The idea is, you declare / instantiate one copy of the variable, and every thread that uses it will get its own copy. No sharing among threads.

That snippet of a statement, that you provided, is part of the discussion about the implementation detail. And quite frankly not that important to understand.... but since you asked... Basically, the copy of the variable for each thread is not store in the thread local class, it is stored in the corresponding thread class. And the data is referenced via weak references, so that if either thread or threadlocal becomes unreachable, the data is gone.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Any disadvantage of thread local variable ? I was asked this in an interview


Groovy
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
One disadvantage is that the object is duplicated for all threads. So if there are lots of threads, or if the object is large in size, there would be memory considerations.

Also, if the threads aren't taken from a thread pool, but are created from scratch each time, then the thread-local object would also be instantiated anew repeatedly. That could be an issue if the object construction is complex or long-winded.

In these cases it may be better to use just a single copy of the object, and properly synchronize access to it.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Well for every variable the thread has a local copy right ? Does it matter if it is declared as thread local variable ?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Pradeep bhatt wrote:Well for every variable the thread has a local copy right ? Does it matter if it is declared as thread local variable ?

Thread-local variables are not declared in the Runnable class, they're declared in some class that is accessed by multiple threads concurrently - like a Servlet.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Another disadvantage is that people don't readily understand the concept of ThreadLocal variables (as this thread shows) and may use them incorrectly.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Lester Burnham wrote:
Pradeep bhatt wrote:Well for every variable the thread has a local copy right ? Does it matter if it is declared as thread local variable ?

Thread-local variables are not declared in the Runnable class, they're declared in some class that is accessed by multiple threads concurrently - like a Servlet.


Every thread has a working memory in which it keeps its own working copy of variables that it must use or assign. As the thread executes a program, it operates on these working copies. The main memory contains the master copy of every variable. There are rules about when a thread is permitted or required to transfer the contents of its working copy of a variable into the master copy or vice versa.


http://java.sun.com/docs/books/jvms/second_edition/html/Threads.doc.html
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
ThreadLocal variables have nothing to do with variables that are local to a Thread object.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Yes You are right. Thread local is per thread basis. Sorry for the confusion.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is thread-local variable