Re: volatile



Timo Nentwig schrieb:
I'm still not completely certain about the volatile keyword.


From cephas.net:

| The volatile modifier requests the Java VM to always access the shared
| copy of the variable so the its most current value is always read. If
| two or more threads access a member variable, AND one or more threads
| might change that variable's value, AND ALL of the threads do not use
| synchronization (methods or blocks) to read and/or write the value,
| then that member variable must be declared volatile to ensure all
| threads see the changed value.


The entire JSE 5 source (3,772,776 lines) uses the "volatile" modifier 111 times. According to my highly scientific analysis, you should not use the "volatile" keyword unless your code base is larger than 34,000 lines :-)


Timo Stamm
.



Relevant Pages

  • Re: volatile
    ... Timo Nentwig schrieb: ... int value; ... void print() ... // IMHO I don't need volatile here: ...
    (comp.lang.java.programmer)
  • Re: is assignment atomic/thread safe?
    ... Peter Duniho wrote: ... Thomas Pornin wrote: ... If thread B reads the volatile variable, then it will see the change ... To do otherwise would violate Java semantics. ...
    (comp.lang.java.programmer)
  • Re: what is the point of volatile?
    ... Fixing double-checked locking was an explicit NON-GOAL for the new ... volatile semantics. ... reads from a volatile variable guarantee that all writes ... That is the new memory model for Java. ...
    (comp.lang.java.help)
  • Re: Petersons Algorithm in java, sequencial instruction execution ?
    ... synchronized blocks and let the JVM sort it out. ... I don't have choice about the language, is to be done in java. ... Which explain the differences between synchronized and volatile only. ... Making everything volatile will generated a boat load of memory barriers. ...
    (comp.lang.java.programmer)
  • Re: Petersons Algorithm in java, sequencial instruction execution ?
    ... the Java memory model is basically like this: ... many other types of fine-grain memory barriers.. ... By putting all volatile i only mean the three variables used ...
    (comp.lang.java.programmer)