| Author |
volatile variable & synchronized
|
Edward Chen
Ranch Hand
Joined: Dec 23, 2003
Posts: 758
|
|
if I have a volatile variable, do I still need to synchronized it ?
In Doc, it said, volatile will force thread NOT to cache object, only one copy in main memory, but it doesn't say concurrent access is safe.
for example,
Thanks.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16809
|
|
Edward Chen wrote:if I have a volatile variable, do I still need to synchronized it ?
In Doc, it said, volatile will force thread NOT to cache object, only one copy in main memory, but it doesn't say concurrent access is safe.
It depends on the operation. A single get or set of the reference is atomic. Anything more than that, is not, and will require you to use the atomic classes (or synchronization).
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32827
|
|
|
Moving topic as not a "beginning" topic.
|
 |
 |
|
|
subject: volatile variable & synchronized
|
|
|