| Author |
Experimenting with "volatile"
|
Reiner Herman
Greenhorn
Joined: Jan 12, 2010
Posts: 19
|
|
Hallo.
I am reading about the happens-before relationship in the JMM trying to understand:
"A write to a volatile field happens-before every subsequent read of that same field"
I have 2 questions.
1) According to this description, why doesnt my code below output 5 ?
Output:
2) In the description what does "subsequent refer to" ? So there is another ordering besides happens-before, how is this defined ?
|
 |
Ireneusz Kordal
Ranch Hand
Joined: Jun 21, 2008
Posts: 423
|
|
Look at this experiment, maybe you'll catch:
On my machine (linux amd 64 2 cores JVM 6) result was:
I stopped program after 1 minute .... surprise - thread 't' didn't see that 'flag' had been changed and was still running !!!
When I marked 'flag' as volatile result was:
This time "A write to a volatile field in the main thread happened-before every subsequent read of that same field in the 't' thread" ;)
On your machine you could get different results ... because threads are not predictable.
But with 'flag' declared as volatile this program will always end.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
|
Moving thread as too difficult for "beginning."
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32675
|
|
Moved to "JiG" forum . . . and welcome to JavaRanch
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Moving again since this is more thread related, and we have a dedicated forum for that.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Kaustav Ganguly
Greenhorn
Joined: Dec 14, 2009
Posts: 28
|
|
try invoking join() method on the runner thread in between line 11 and 12 , then the output should be 5.
Actually we have two threads here , the main thread and the one you are creating , now which one will be executed first is not gauranted.
So try using join as following
|
 |
Reiner Herman
Greenhorn
Joined: Jan 12, 2010
Posts: 19
|
|
Ireneusz Kordal wrote:
But with 'flag' declared as volatile this program will always end.
But that is what I don't understand since I do have declared the id volatile.
Kaustav Ganguly wrote:try invoking join() method on the runner thread in between line 11 and 12 , then the output should be 5.
It is not that I want the output to be 5, but that I am trying to understand what volatile does and it seems to do something from what I excpect the way I read the description of the JMM.
How about that "subsequent" thing ? When does soething subseed something else when it is not the happens-before relation that is the ordering ?
|
 |
Reiner Herman
Greenhorn
Joined: Jan 12, 2010
Posts: 19
|
|
I tried the following wich confuses me more, inside run I printout the id before changing it and now the program seems to consistently outputs 5. By the way the first example seemed to always output -4 but at times outputs 5.
This seem to consistently output either:
or
|
 |
Reiner Herman
Greenhorn
Joined: Jan 12, 2010
Posts: 19
|
|
|
I still haven't figured out what is happening. Would really like a hint from someone who knows Javas memory model "cold".
|
 |
Rok Ć telcer
Ranch Hand
Joined: Nov 03, 2009
Posts: 101
|
|
Hi,
Calling a start on a newly created thread, doesn't mean that is is already running (state=runnable).
It just means that it is ready to run and it is up to the thread scheduler to decide when it will actually run/invoke it (state=running).
So in your example, the setId method is called before the thread has actually started (you could even say that you set it in a single threaded env.).
Ireneusz Kordal wrote: This time "A write to a volatile field in the main thread happened-before every subsequent read of that same field in the 't' thread" ;)
I'm not sure from where did you get the "main thread" part.
Read the following link Threads and Locks.
... second discussion after section "17.4.5 Happens-before Order"
Hope this helps.
Regards,
Rok
|
SCJP, SCWCD
|
 |
Reiner Herman
Greenhorn
Joined: Jan 12, 2010
Posts: 19
|
|
Thank you for the reply.
This is from the Java Doc on threads start method: "Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread."
So the call does not make the status of the thread "running" ?
And from 17.4.4 in the specs:
"An action that starts a thread synchronizes-with the first action in the thread it starts."
I am still confused...
Maybe I have to study the spec thoroughly.
|
 |
abhay bansal
Greenhorn
Joined: Nov 26, 2009
Posts: 8
|
|
|
The new Thread().start() method calls the native start method. It is in the hands of OS when the thread will start running. In this particular example the two threads are independent when you call the start method, it might give different result each time you run it.
|
 |
 |
|
|
subject: Experimenting with "volatile"
|
|
|