[concurrency-interest] Relativity of guarantees provided by volatile

Jan Nielsen jan.sture.nielsen at gmail.com
Sat Aug 18 12:13:36 EDT 2012


On Fri, Aug 17, 2012 at 3:24 PM, Marko Topolnik <mtopolnik at inge-mark.hr>
 wrote:

> 2. If there is no loophole, is there anything to worry about, given that
> practically 100% developers out there consider as guaranteed something that
> isn't?


I'm part of the 99%. When I read: "A field may be declared volatile, in
which case the Java Memory Model ensures that all threads see a consistent
value for the variable" [1] I understand this to mean for volatile variable
's' updated in one thread and read from another, these scenarios are
possible:

   Thread0
0: volatile int s=0;

   Thread0  Thread1
-----------------
1: s=1
2:          s==1
3: s=2
4:          s==2
5: s=3
6:          s==3
-----------------
1:          s==1
2:          s==1
3:          s==1
4: s=1
5: s=2
6: s=3
-----------------
1: s=1
2: s=2
3: s=3
4:          s==3
5:          s==3
6:          s==3

[1] http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.4
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120818/13100b11/attachment.html>


More information about the Concurrency-interest mailing list