java-memory-model
What does “volatile” mean in Java?
Short of reading the memory model specification, I recommend you read http://jeremymanson.blogspot.com/2008/11/what-volatile-means-in-java.html. It’s written by one of the Java Memory Model authors and should answer your question. Thinking of memory reads and writes in terms of the happens-before clause is also helpful; the JMM for Java 5 onwards adds happens-before semantics to volatile. Specifically, when … Read more
Behavior of memory barrier in Java
Doug Lea is right. You can find the relevant part in section §17.4.4 of the Java Language Specification: §17.4.4 Synchronization Order [..] A write to a volatile variable v (§8.3.1.4) synchronizes-with all subsequent reads of v by any thread (where “subsequent” is defined according to the synchronization order). [..] The memory model of the concrete … Read more
Java memory model: volatile variables and happens-before
i = 1 always happens-before v = 2 True. By JLS section 17.4.5, If x and y are actions of the same thread and x comes before y in program order, then hb(x, y). v = 2 happens-before vDst = v in JMM only if it’s actually happens before in time i = 1 happens-before … Read more
Immutability and reordering
The confusion I think you have here is what the author meant by safe publication. He was referring to the safe publication of a non-null Resource, but you seem to get that. Your question is interesting – is it possible to return a null cached value of resource? Yes. The compiler is allowed to reorder … Read more
Volatile guarantees and out-of-order execution [duplicate]
No, you will never get a NPE. This is because volatile also has the memory-effect of introducing a happens-before relationship. In other words, it will prevent reordering of a = one; b = two; The statements above, will not be re-ordered, and all threads will observe value one for a if b already has value … Read more
What is a TLAB (Thread Local Allocation Buffer)?
The idea of a TLAB is to reduce the need of synchronization between threads. Using TLABs, this need is reduced as any thread has an area it can use and expect that it is the only thread using this area. Assuming that a TLAB can hold 100 objects, a thread would only need to aquire … Read more
Memory effects of synchronization in Java
The short answer is that JSR-133 goes too far in its explanation. This isn’t a serious issue because JSR-133 is a non-normative document which isn’t part of the language or JVM standards. Rather, it is only a document which explains one possible strategy that is sufficient for implementing the memory model, but isn’t in general … Read more
Dalvik VM & Java Memory Model (Concurrent programming on Android)
I haven’t read your question completely, but first of all do not use volatile, even opengles coders do not use it for different ui vs renderer threads. Use volatile if and only if one thread writes (say to some class’ static property) and other reads, even then you have to synchronize, read this for some … Read more
What are the similarities between the Java memory model and the C++11 memory model?
The Java memory model was an important influence on the C++11 memory model, and was where we pulled the terms happens-before and synchronizes-with from. However, the C++11 memory model offers much more fine-grained control over memory ordering than the Java memory model. Java volatile variables are equivalent to C++11 std::atomic<> variables, if you use std::memory_order_acquire … Read more