• Post Reply Bookmark Topic Watch Topic
  • New Topic

java memory model and volatile

 
Piotr Nowakowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've got another question.
This time with the volatile.

Here is a code:

Why is it possible to print "NOT OK" ?
According to the Java specification - "a volatile write in one thread happens-before any subsequent volatile read in another thread".
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why shouldn't it be possible? All that needs to happen is that thread "1" reaches its line 17 before thread "0" reaches its line 15.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Why shouldn't it be possible? All that needs to happen is that thread "1" reaches its line 17 before thread "0" reaches its line 15.


And of course, when that happens, the read at line 17 by thread "1" is *not* considered a "subsequent read"...

Henry
 
Piotr Nowakowski
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but can you please provide any example in which we can observe the "happens-before" partial ordering using volatile.
I think I don't quite understand the "happens-before" and I cannot find any good explanation of it.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there are several threads that are eligible to run you can't predict which one the scheduler will choose. But you can sort of simulate control passing from one thread to the other at a particular spot in the code by calling Thread.yield().
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piotr Nowakowski wrote:Ok, but can you please provide any example in which we can observe the "happens-before" partial ordering using volatile.
I think I don't quite understand the "happens-before" and I cannot find any good explanation of it.


I don't know if I can provide an example, but here is an explanation of the statement (good or otherwise).

Piotr Nowakowski wrote:
According to the Java specification - "a volatile write in one thread happens-before any subsequent volatile read in another thread".


Let's break down what the specification is saying. It is saying, that with volatile variables, and in a threaded environment, then ...

"A write must happen before any read that happens later"

Now, if you think about it, isn't that completely obvious? Shouldn't all writes happen before reads that happen later? Believe it or not, in a threaded environment, this is not guaranteed with non-volatile variables. It is possible for a later read to get the older value, as if it happened before the write.

There are a few reasons for this. First, memory are cached. When you write to memory, the hardware may not immediate write through all the way to memory. There may be three layers of cache that it must go through, and it is not very efficient to go through all of them (and at the same time, invalidate that cache line for all the other cores/processors). The processor may just write to the cache (and mark the line as dirty), and let the cache hardware eventually flush it out to memory.

Second, on the read side, the variable may be optimized to a register instead of memory. For example, if you are using a variable in a tight loop, there is no reason to constantly read that variable, even if it is cached. You could just read it once to a register, and use the value in the register for the loop.

Anyway, with volatile variables, neither of these optimizations are allowed to happen.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!