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

Strange behaviour between 32 and 64 bit  RSS feed

 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason, this works on a 32 bit machine, but does not on a 64 bit. On the 64 bit, it hangs in the while loop. To correct this, a System.out.println (or possibly other statements) must be put in the while loop for it to exit the loop. Any System print will do (does not have to access the boolean). Again, on the 32 bit, it runs to completion without issue.



Any idea why this is happening?

Edit: This is tested using the latest version of Java.
[ March 05, 2008: Message edited by: Brad Walton ]
 
Henry Wong
author
Sheriff
Posts: 22837
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This line of code is spinning on a boolean waiting for it to change. However, the boolean is not volatile, nor are there any synchronization happening between the threads. So it is possible for the JVM to cache (optimize) the boolean, and hence, never see the change -- even though the boolean is changed by the other thread.

Obviously, the fix is to either make the boolean volatile or add synchronization. But with some JVMs, calling methods may sometime work (which is what System.out.print() does). With some JVMs, the method call causes the boolean to invalidate, causing a reload of it in the next loop.

Henry
 
Brad Walton
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply and explanation Henry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!