Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can operations on volatile variables be reodered  RSS feed

 
chatchai chailuecha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

I am reading "Java Threads 3rd edition"(Oreilly). The book covers J2SE1.5 so it must also cover thread programming in the new JMM.
In chapter5 at "The effect of reodering statements", there is an example code as shown below.



Then the author explain that this can be a problem if JVM decide to do some reordering.

Thread1: Update total score
Thread1: Set currentScore == 0
Thread2: See if currentScore == 0
Thread2: Return finalScore
Thread1: Update finalScore

Then the author state that
"It doesn't make any different whether the variables are defined as volatile: statements that include volatile variables can be reordered just like any other statements"

As far as I know, if the currentScore is volatile then JMM must guarantee the happens-before relationship. If the second thread see the update of currentScore then the write to finalScore must happens before the write to currentScore and the second thread should return the correct value of finalScore.

Do I misunderstand something?

Regards,
Chatchai Chailuecha
www.devguli.com

[ February 07, 2008: Message edited by: chatchai chailuecha ]
[ February 07, 2008: Message edited by: chatchai chailuecha ]
 
Henry Wong
author
Sheriff
Posts: 22827
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I misunderstand something?


No. You got it right. I surprised that we accidently let this one get by.

Henry
 
chatchai chailuecha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thanks very much for your reply. I am more convinced in the principle now.

Chatchai
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to remember though is that volatile variables can be reordered with respect to non-volatile variables.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"One thing to remember though is that volatile variables can be reordered with respect to non-volatile variables. "


eh ?? no they can't post JSR 133 (apologies if I misunderstood your point)...

Under the new memory model, accesses to volatile variables cannot be reordered with each other or nonvolatile variable accesses. Writing to a volatile field has the same memory effect as a monitor release, and reading from a volatile field has the same memory effect as a monitor acquire. In effect, because the new memory model places stricter constraints on reordering of volatile field accesses with other field accesses, volatile or not, anything that was visible to thread A when it writes to volatile field f becomes visible to thread B when it reads f."


http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#volatile
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!