Questions tagged [java-memory-model]

The Java Memory Model (JMM) describes which executions of a program are legal by determining what value(s) can be observed when reading a shared variable according to certain rules.

The execution of a single threaded program is intuitive as it follows the program order (i.e. the order in which the statements are laid out in the source code).

However, when a program uses several threads and those threads share information, the execution order can become unintuitive due to instructions reordering, partially constructed objects, same variable having different values in different threads because of caching at the CPU level etc.

The Java Memory Model (JMM) describes which executions of a program are legal by determining what value(s) can be observed when reading a shared variable according to certain rules, which are defined in the Chapter 17 of the Java Language Specification (JLS), more specifically in section 17.4.

384 questions
210
votes
5 answers

Why does this Java program terminate despite that apparently it shouldn't (and didn't)?

A sensitive operation in my lab today went completely wrong. An actuator on an electron microscope went over its boundary, and after a chain of events I lost $12 million of equipment. I've narrowed down over 40K lines in the faulty module to…
Dog
  • 7,707
  • 8
  • 40
  • 74
63
votes
4 answers

How can CopyOnWriteArrayList be thread-safe?

I've taken a look into OpenJDK source code of CopyOnWriteArrayList and it seems that all write operations are protected by the same lock and read operations are not protected at all. As I understand, under JMM all accesses to a variable (both read…
Fixpoint
  • 9,619
  • 17
  • 59
  • 78
58
votes
1 answer

Instruction reordering & happens-before relationship

In the book Java Concurrency In Practice, we are told several time that the instructions of our program can be reordered, either by the compiler, by the JVM at runtime, or even by the processor. So we should assume that the executed program will not…
Martin Pernollet
  • 2,285
  • 1
  • 28
  • 39
54
votes
2 answers

Why is `synchronized (new Object()) {}` a no-op?

In the following code: class A { private int number; public void a() { number = 5; } public void b() { while(number == 0) { // ... } } } If method b is called and then a new thread is…
yankee
  • 38,872
  • 15
  • 103
  • 162
52
votes
3 answers

What is in Java object header?

Could you give me some information on what is exactly stored in object header? I know, that it's probably JVM dependent, but maybe for HotSpot at least? I'm looking for exact description specifically for a first row. I've read several information…
alobodzk
  • 1,284
  • 2
  • 15
  • 27
51
votes
8 answers

Must all properties of an immutable object be final?

Must immutable objects have all properties be final? I would say they don't. But I don't know whether I am right or not.
DRastislav
  • 1,892
  • 3
  • 26
  • 40
48
votes
1 answer

What are the similarities between the Java memory model and the C++11 memory model?

The new C++ standard introduces the notion of a memory model. There were already questions on Stack Overflow about it, what does it mean, how does it change the way we write code in C++ and so on. I'm interested in getting to know how does the C++…
ciamej
  • 6,918
  • 2
  • 29
  • 39
47
votes
4 answers

Dalvik VM & Java Memory Model (Concurrent programming on Android)

I am working on Android projects which involve the lot of concurrent programming and I am going to implement some custom inter-threads communication stuff (the one from java.util.concurent are not well suited for my purposes). The concurrent…
Alexey Kryshen
  • 1,067
  • 2
  • 11
  • 15
47
votes
1 answer

What is a TLAB (Thread Local Allocation Buffer)?

I couldn't find a comprehensive source that would explain the concept in a clear manner. My understanding is that a thread is given some chunk of memory in the eden where it allocates new objects. A competing thread will end up having a somewhat…
user1745356
  • 4,462
  • 7
  • 42
  • 70
45
votes
6 answers

Memory effects of synchronization in Java

JSR-133 FAQ says: But there is more to synchronization than mutual exclusion. Synchronization ensures that memory writes by a thread before or during a synchronized block are made visible in a predictable manner to other threads which …
Binil Thomas
  • 13,699
  • 10
  • 57
  • 70
43
votes
5 answers

Volatile guarantees and out-of-order execution

IMPORTANT EDIT I know about the "happens before" in the thread where the two assignments are happening my question is would it be possible for another thread to be reading "b" non-null while "a" is still null. So I know that if you're calling doIt()…
SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
42
votes
10 answers

Immutability and reordering

The code below (Java Concurrency in Practice listing 16.3) is not thread safe for obvious reasons: public class UnsafeLazyInitialization { private static Resource resource; public static Resource getInstance() { if (resource ==…
assylias
  • 321,522
  • 82
  • 660
  • 783
39
votes
3 answers

Java memory model: volatile variables and happens-before

I'd like to clarify how happens-before relation works with volatile variables. Let we have the following variables: public static int i, iDst, vDst; public static volatile int v; and thread A: i = 1; v = 2; and thread B: vDst = v; iDst = i; Are…
36
votes
2 answers

Behavior of memory barrier in Java

After reading more blogs/articles etc, I am now really confused about the behavior of load/store before/after memory barrier. Following are 2 quotes from Doug Lea in one of his clarification article about JMM, which are both very…
asticx
  • 643
  • 2
  • 8
  • 14
35
votes
6 answers

What does "volatile" mean in Java?

We use volatile in one of our projects to maintain the same copy of variable accessed by different threads. My question is whether it is alright to use volatile with static. The compiler does not give any errors but I don't understand the reason of…
jnivasreddy
  • 517
  • 2
  • 9
  • 16
1
2 3
25 26