Skip to content

Commit 09a12f9

Browse files
committed
docs: add English README oldratlee#2
1 parent 21d8672 commit 09a12f9

File tree

2 files changed

+204
-3
lines changed

2 files changed

+204
-3
lines changed

README.md

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# 🎏 fucking-java-concurrency
2+
3+
📖 English Documentation | [📖 中文文档](docs/zh-CN/README.md)
4+
5+
Simple showcases of `Java` concurrency problems, seeing 🙈 is believing 🐵.
6+
7+
## 🍎 Reasons to organize Demo
8+
9+
- The actual phenomenon that can be observed 🙈 is more intuitive and more trustworthy than the concurrency principle mentioned 🙊.
10+
- The `Java` language standard library supports threads, and multithreading is heavily used in the language itself (such as `GC`) and applications (the Server side).
11+
- Concurrency program design, in the analysis and implementation, the complexity is greatly increased. If you do not fully understand and systematically analyze the concurrent logic, and write code at will, it is not an exaggeration to describe such a program as "**accidentally**" running with the correct result.
12+
The demos here do not give explanations and discussions, and they are all entry-level :neckbeard: . For more information, please refer to the concurrent materials by yourself.
13+
14+
Examples of concurrency problems you encountered in development are welcome to provide ([submit Issue](https://github.com/oldratlee/fucking-java-concurrency/issues)) or share ([pull request after Fork](https://github.com/oldratlee/fucking-java-concurrency/fork))! 😘
15+
16+
----------------------------------------
17+
18+
<img src="docs/dining-philosophers-problem.jpg" width="30%" align="right" />
19+
20+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
21+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
22+
23+
- [🍺 Synchronized update cannot be read in another thread](#-synchronized-update-cannot-be-read-in-another-thread)
24+
- [Demo description](#demo-description)
25+
- [Problem statement](#problem-statement)
26+
- [Quickly run](#quickly-run)
27+
- [🍺 Infinite loop of HashMap](#-infinite-loop-of-hashmap)
28+
- [Demo description](#demo-description-1)
29+
- [Problem statement](#problem-statement-1)
30+
- [Quickly run](#quickly-run-1)
31+
- [🍺 Combination state read invalid combination](#-combination-state-read-invalid-combination)
32+
- [Demo description](#demo-description-2)
33+
- [Problem statement](#problem-statement-2)
34+
- [Quickly run](#quickly-run-2)
35+
- [🍺 `long` variable read invalid value](#-long-variable-read-invalid-value)
36+
- [Demo description](#demo-description-3)
37+
- [Problem statement](#problem-statement-3)
38+
- [Quickly run](#quickly-run-3)
39+
- [🍺 Unsynchronized concurrency count result is wrong](#-unsynchronized-concurrency-count-result-is-wrong)
40+
- [Demo description](#demo-description-4)
41+
- [Problem statement](#problem-statement-4)
42+
- [Quickly run](#quickly-run-4)
43+
- [🍺 Synchronization over volatile domains](#-synchronization-over-volatile-domains)
44+
- [Demo description](#demo-description-5)
45+
- [Problem statement](#problem-statement-5)
46+
- [Quickly run](#quickly-run-5)
47+
- [🍺 Symmetric lock deadlock](#-symmetric-lock-deadlock)
48+
- [Demo description](#demo-description-6)
49+
- [Problem statement](#problem-statement-6)
50+
- [Quickly run](#quickly-run-6)
51+
52+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
53+
54+
----------------------------------------
55+
56+
## 🍺 Synchronized update cannot be read in another thread
57+
58+
Demo class [`com.oldratlee.fucking.concurrency.NoPublishDemo`](src/main/java/com/oldratlee/fucking/concurrency/NoPublishDemo.java).
59+
60+
### Demo description
61+
62+
Set the field `stop` to `true` in the `main` thread to control the exit of the task thread started in `main`.
63+
64+
### Problem statement
65+
66+
After the `main` thread field `stop` is `true`, the task thread continues to run, that is, no new value has been read in the task thread.
67+
68+
### Quickly run
69+
70+
```bash
71+
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.NoPublishDemo
72+
```
73+
74+
## 🍺 Infinite loop of HashMap
75+
76+
This problem has been explained in many places.
77+
78+
79+
80+
The Demo class [`com.oldratlee.fucking.concurrency.HashMapHangDemo`](src/main/java/com/oldratlee/fucking/concurrency/HashMapHangDemo.java) can reproduce this problem.
81+
82+
### Demo description
83+
84+
Two task threads are opened in the main thread to perform the put operation of HashMap. The main thread does the get operation.
85+
86+
### Problem statement
87+
88+
The main thread Block is determined by no continuous output, that is, the endless loop of HashMap appears.
89+
90+
### Quickly run
91+
92+
```bash
93+
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.HashMapHangDemo
94+
```
95+
96+
## 🍺 Combination state read invalid combination
97+
98+
When programming, multiple state records will be required (state can be a `POJO` object or `int`, etc.).
99+
100+
It is often seen that the multi-state read and write code is not synchronized, and the person who write it will naturally ignore the issue of thread safety.
101+
102+
Invalid combinations are combinations that have never been set.
103+
104+
### Demo description
105+
106+
The main thread modifies multiple states. For the convenience of checking, each write has a fixed relationship: the second state is twice the value of the first state. Read multiple states in a task thread.
107+
Demo class com.oldratlee.fucking.concurrency.InvalidCombinationStateDemo.
108+
109+
### Problem statement
110+
111+
The second state read in the task thread is not twice the value of the first state, that is, an invalid value.
112+
113+
### Quickly run
114+
115+
```bash
116+
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.InvalidCombinationStateDemo
117+
```
118+
119+
## 🍺 `long` variable read invalid value
120+
121+
An invalid value is a value that has never been set.
122+
123+
Reading and writing of `long` variables is not atomic and will be divided into two 4-byte operations.
124+
125+
Demo class [`com.oldratlee.fucking.concurrency.InvalidLongDemo`](src/main/java/com/oldratlee/fucking/concurrency/InvalidLongDemo.java).
126+
127+
### Demo description
128+
129+
The main thread modifies the long variable. For the convenience of checking, the upper 4 bytes and the lower 4 bytes of the long value written each time are the same. Read the long variable in the task thread.
130+
131+
### Problem statement
132+
133+
In the task thread, a long variable whose upper 4 bytes and lower 4 bytes are different is read, which is an invalid value.
134+
135+
### Quickly run
136+
137+
```bash
138+
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.InvalidLongDemo
139+
```
140+
141+
## 🍺 Unsynchronized concurrency count result is wrong
142+
143+
Demo class [`com.oldratlee.fucking.concurrency.WrongCounterDemo`](src/main/java/com/oldratlee/fucking/concurrency/WrongCounterDemo.java).
144+
145+
### Demo description
146+
147+
Two task threads are opened in the main thread to execute concurrent incrementing counts. Main thread final result check.
148+
149+
### Problem statement
150+
151+
The count value is incorrect.
152+
153+
### Quickly run
154+
155+
```bash
156+
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.WrongCounterDemo
157+
```
158+
159+
## 🍺 Synchronization over volatile domains
160+
161+
It is common to see synchronization code on a volatile domain, and person who write it will naturally feel that this is safe and correct.
162+
\# For problem analysis, see the article [Synchronization on mutable fields](http://www.ibm.com/developerworks/library/j-concurrencybugpatterns/#N100E7).
163+
164+
Demo class [`com.oldratlee.fucking.concurrency.SynchronizationOnMutableFieldDemo`](src/main/java/com/oldratlee/fucking/concurrency/SynchronizationOnMutableFieldDemo.java).
165+
166+
### Demo description
167+
168+
Two task threads are opened in the main thread to execute addListener. Main thread final result check.
169+
170+
### Problem statement
171+
172+
The final number of Listeners is incorrect.
173+
174+
### Quickly run
175+
176+
```bash
177+
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.SynchronizationOnMutableFieldDemo
178+
```
179+
180+
## 🍺 Symmetric lock deadlock
181+
182+
\# For problem analysis, see the article [Synchronization on mutable fields](http://www.ibm.com/developerworks/library/j-concurrencybugpatterns/#N101C1)
183+
184+
Demo class [`com.oldratlee.fucking.concurrency.SymmetricLockDeadlockDemo`](src/main/java/com/oldratlee/fucking/concurrency/SymmetricLockDeadlockDemo.java).
185+
186+
### Demo description
187+
188+
Two task threads are opened in the main thread for execution.
189+
190+
### Problem statement
191+
192+
Task thread deadlocked.
193+
194+
### Quickly run
195+
196+
```bash
197+
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.SymmetricLockDeadlockDemo
198+
```

docs/zh-CN/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.NoPubli
8080
🍺 `HashMap`的死循环
8181
----------------------------------
8282

83-
这个问题在[疫苗:Java HashMap的死循环](http://coolshell.cn/articles/9606.html)等多个地方都有讲解。
83+
这个问题在[疫苗:Java HashMap的死循环](http://coolshell.cn/articles/9606.html)等多个地方都有讲解。
84+
8485
Demo类[`com.oldratlee.fucking.concurrency.HashMapHangDemo`](../../src/main/java/com/oldratlee/fucking/concurrency/HashMapHangDemo.java),可以复现这个问题。
8586

8687
### Demo说明
@@ -124,7 +125,8 @@ mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.Invalid
124125

125126
无效值 是指 从来没有设置过的值。
126127

127-
`long`变量读写不是原子的,会分为2次4字节操作。
128+
`long`变量读写不是原子的,会分为2次4字节操作。
129+
128130
Demo类[`com.oldratlee.fucking.concurrency.InvalidLongDemo`](../../src/main/java/com/oldratlee/fucking/concurrency/InvalidLongDemo.java)
129131

130132
### Demo说明
@@ -164,7 +166,8 @@ mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.WrongCo
164166
-------------------------
165167

166168
常看到在易变域上的同步代码,并且写的同学会很自然觉得这样是安全和正确的。
167-
\# 问题分析见文章链接:[在易变域上的同步](http://www.ibm.com/developerworks/cn/java/j-concurrencybugpatterns/#N100DA),对应的英文文章:[Synchronization on mutable fields](http://www.ibm.com/developerworks/library/j-concurrencybugpatterns/#N100E7)
169+
\# 问题分析见文章链接:[在易变域上的同步](http://www.ibm.com/developerworks/cn/java/j-concurrencybugpatterns/#N100DA),对应的英文文章:[Synchronization on mutable fields](http://www.ibm.com/developerworks/library/j-concurrencybugpatterns/#N100E7)
170+
168171
Demo类[`com.oldratlee.fucking.concurrency.SynchronizationOnMutableFieldDemo`](../../src/main/java/com/oldratlee/fucking/concurrency/SynchronizationOnMutableFieldDemo.java)
169172

170173
### Demo说明

0 commit comments

Comments
 (0)