• 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

Regarding ThreadLocal and Volatile

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
can you explain to me what is difference between ThreadLocal and Volatile?and when to use ThreadLocal class and Volatile?
Actually I'm confusing with these two concepts.
 
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

surya.raaj prakash wrote:can you explain to me what is difference between ThreadLocal and Volatile?and when to use ThreadLocal class and Volatile?
Actually I'm confusing with these two concepts.



They are not really related. The ThreadLocal class is a class that stores a variable in the Thread object of the current running thread, hence, while each thread can share the same ThreadLocal instance, they are not really sharing the underlying variable at all.

The volatile keyword tells the compiler that the variable is shared, and likely shared without synchronization (or just treat it as such)... Caching of the variable in registers will not be allowed (reads direct from memory, writes go straight to memory), certain code motion optimization will not be allowed, and in the case of long and double, the JVM will make sure that read and writes are atomic, even if the underlying hardware don't support 64 bit atomic operations.

Henry
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic