문제

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.

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top