This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is thread-local variable

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any disadvantage of thread local variable ? I was asked this in an interview
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20756
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ThreadLocal variables have nothing to do with variables that are local to a Thread object.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes You are right. Thread local is per thread basis. Sorry for the confusion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic