• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is thread-local variable

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any disadvantage of thread local variable ? I was asked this in an interview
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes You are right. Thread local is per thread basis. Sorry for the confusion.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic