This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Using volatile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Using volatile" Watch "Using volatile" New topic
Author

Using volatile

Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188




0.109 Thread-0 entering run()
0.203 main entering workMethod()
0.203 main in workMethod() - about to sleep for 2 seconds
2.203 main in workMethod() - just set value=50
2.203 main in workMethod() - about to sleep for 5 seconds
2.203 Thread-0 leaving run() missedIt false value 50
7.203 main in workMethod() - just set missedIt=true
7.203 main in workMethod() - about to sleep for 3 seconds
10.203 main leaving workMethod()





Why does the below code not get executed ?






value (variable) is not volatile then how does it break out of the while loop when its value gets changed in workMethod ?

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

I am not exactly sure I know what your question here. But if I take this literally:
"value (variable) is not volatile then how does it break out of the while loop when its value gets changed in workMethod"
And without looking at your code (I did look at your code though), then I would have to respond: Data can get published between threads without volatile, so you could see value change in one thread as another thread updates it, even if it weren't volatile (and if there was nothing else going on). The difference between volatile and non-volatile is guarantees. With a volatile variable you are guaranteed to see the other thread's updates and you are guaranteed that data actions are performed in a 'happens-before' (and happens-after) relationship relative to the volatile variable, with non-volatile you are not guaranteed anything about timing or order.


Also:
"Why does the below code not get executed ? "
Because all of that is inside the if(missedIt) statement. It is apparent from the timestamps that the value is read as being changed prior to the missedIt flag being set.

Steve
Sudarshan Sreenivasan
Ranch Hand

Joined: Jun 28, 2007
Posts: 188

Hi Steve,

The below mentioned sentence answers my query , I was under the impression that non-volatile variables will not be updated.may or may not be updated is more correct.

and for volatile variables will be updated.

The difference between volatile and non-volatile is guarantees
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using volatile
 
Similar Threads
Insights into synchronization
Experimenting with "volatile"
Exam 9, Q24 Dan Chisholm
not at all able to understand what is happening in this program
thread communication