Question

Is it safe to say that a ThreadLocal variable is conceptually the 'opposite' of a volatile variable in Java? Because in the former, every thread will only read/updatea local copy of the variable. But in the latter, every thread will always see the latest value of the variable that is in main memory....no chance of seeing stale values.

Was it helpful?

Solution

Exactly but with a slight twist,

  • a ThreadLocal variable is a variable different for each thread
  • any other variable exists only once so common for each Thread using the same object, whether it is volatile or not.

Volatile however specifies some kind of thread read/write boundary so they must synchronize on the latest value written by any other thread. However using volatile does not ensure thread safety.

E.g. a increment method incrementing a volatile int might still generate duplicates. To be sure that works thread safe you must synchronize the method updating the volatile attributes!

check this for more detailed information: https://www.cs.umd.edu/users/pugh/java/memoryModel/jsr-133-faq.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top