Skip to content

Commit 195321b

Browse files
committed
java memory model explanation added
1 parent 6403e92 commit 195321b

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Concurrency Patterns and features found in Java, through multithreaded programmi
2525
* Single Thread Pool
2626
* Scheduled Thread Pool
2727
* Atomics
28+
* Java Memory Model
2829

2930
## Patterns
3031
* Protect Shared State
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package br.com.leonardoz.features.java_memory_model;
2+
3+
/**
4+
=== Introduction ===
5+
6+
This will not include any Java concurrency feature, but a briefly explanation
7+
on what is the Java Memory Model (JMM).
8+
9+
The Java Memory Model specifies the "minimal guarantees" on how about reads and
10+
writes happens on shared variables. The JMM is an abstraction of the multiples existing memory models in processor architectures.
11+
12+
== Reordering ==
13+
14+
When data is shared across threads, there's not so much guarantees on the
15+
execution order by the threads, and because of the imprevisibility, it's import to identify shared data
16+
and to use a proper synchronization mechanism to ensure order and to keep the
17+
visibility guarantees provided by the JMM.
18+
19+
== Happens-Before ==
20+
21+
The JMM is defined in terms of actions, and some operations can have
22+
"Happens-Before" relationship. In Happens-Before, if one action
23+
"happens-before" another action, then everything done in the first action is
24+
visible to the second.
25+
26+
In other terms, the write operations done by one thread are guaranteed to be
27+
visible to a read coming from another thread if the write operation
28+
"happens-before" the read operation.
29+
30+
Without the happen-before relationship, the JVM is free to reorder the
31+
actions as it pleases.
32+
33+
== Happens-Before rules ==
34+
35+
Some structures have Happens-Before rules associated with it (text from JCP
36+
book).
37+
38+
Program order rule: Actions in a thread "happens-before" every action in
39+
that thread* that comes later in the program order. - "Keep the sequence of
40+
instructions, if executed in the same thread".
41+
42+
Monitor lock/Explicit lock rule: An unlock on a lock "happens-before" every
43+
subsequent lock on that same monitor lock. - "Locks only after it unlocks".
44+
45+
Volatile variable/Atomics rule: A write to a volatile or atomic field
46+
"happens-before" every subsequent read of that same field. - "Writes happens-before
47+
reads in volatile/atomic fields".
48+
49+
Thread start rule: A call to "Thread.start" in a thread "happens-before"
50+
than every action on the started thread. - "Thread.start happens first than other
51+
actions".
52+
53+
Thread termination rule: Any action in a thread "happens-before" any other
54+
thread detects that thread has terminated, either by successfully return from
55+
"Thread.join" or by "Thread.isAlive" returning false. - "Thread A will
56+
execute everything before another Thread call .join on with."
57+
58+
Interruption rule: A thread calling interrupt on another thread
59+
"happens-before" the interrupted thread detects the interrupt (either by
60+
having InterruptedException thrown, or invoking isInterrupted or
61+
interrupted).
62+
63+
Finalizer rule: The end of a constructor for an object "happens-before" the
64+
start of the finalizer for that object.
65+
66+
Transitivity. If A happens-before B, and B happens-before C, then A
67+
happens-before C.
68+
69+
Other rules from java library classes:
70+
71+
- Insert items in a thread-safe collection happens-before access or removal of
72+
items;
73+
74+
- Actions in a thread happens-before than Runnable/Callable being sent to an Executor;
75+
76+
- Actions taken by a asynchronous computation like Future happens-before another threads calls Future.get;
77+
78+
- CountDownLatch.countDown happens-before a thread returns from await on this
79+
latch;
80+
81+
- A Thread arriving at a CyclicBarrier happens-before the other threads are
82+
released from that barrier exchange point or the barrier release action is
83+
executed.
84+
85+
*/
86+
public class WhatIsJavaMemoryModel {
87+
88+
}

0 commit comments

Comments
 (0)