Re: Interview Questions



Lew wrote:
Lew wrote:
Daniel Pitts wrote:
Q: What is the affect of the volatile keyword in Java 1.5? Has it changed since 1.2?
A: In java 1.5, the a read from a volatile happens-before the write of the value that was read. This is not necessarily the semantics of volatile before 1.5.

Very summarized and, of course, completely correct.

Drat, I misread the post and misspoke. It is not completely correct. Daniel's statement is actually not at all correct. Arne's post woke me up.

A write from thread A to a volatile variable that precedes a read from thread B /happens-before/ that read, and all writes of any variables from A that preceded the write to the volatile variable from A /happen-before/ all reads of any variables from B subsequent to that read of the volatile in B.

Heh, I mis-wrote it actually. I *meant* the converse of what I wrote.
A write to a volatile happens-before the read of the value that was written.

Oops. Other than a little dyslexia, I was correct :-)

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
.



Relevant Pages

  • Re: Interview Questions
    ... In java 1.5, the a read from a volatile happens-before the write of the value that was read. ... If they go on to mention that the cost of 'synchronized', and of synchronization generally, plummeted in Java 5 and later, so much the better. ...
    (comp.lang.java.programmer)
  • Re: Interview Questions
    ... the a read from a volatile happens-before the write of the value that was read. ... Arne's post woke me up. ... A write from thread A to a volatile variable that precedes a read from thread B /happens-before/ that read, and all writes of any variables from A that preceded the write to the volatile variable from A /happen-before/ all reads of any variables from B subsequent to that read of the volatile in B. ...
    (comp.lang.java.programmer)
  • Re: Petersons Algorithm in java, sequencial instruction execution ?
    ... Alls you need are memory barriers. ... Volatile declarations for the implementing variables should be sufficient. ... corresponds to G. ... If A precedes B, then P must precede Q. (Less ...
    (comp.lang.java.programmer)