fucking-java-concurrency
fucking-java-concurrency copied to clipboard
π Simple showcases of java concurrency problems, seeing π is believing π΅
π fucking-java-concurrency
π English Documentation | π δΈζζζ‘£
Simple showcases of Java
concurrency problems, seeing π is believing π΅.
π Reasons to organize Demo
- The actual phenomenon that can be observed π is more intuitive and more trustworthy than the concurrency principle mentioned π.
- The
Java
language standard library supports threads, multithreading is heavily used in the language itself (such asGC
) and applications (the server side). - 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.
- The demos here do not give explanations and discussions, and they are all entry-level :neckbeard: . For more information, please loop up the concurrency materials by yourself.
Examples of concurrency problems you encountered in development are welcome to provide (submit Issue) or share (pull request after Fork)! π

-
πΊ Update without synchronization cannot be read in another thread
- Demo description
- Problem statement
- Quickly run
-
πΊ Infinite loop of
HashMap
- Demo description
- Problem statement
- Quickly run
-
πΊ Combined state read invalid combination
- Demo description
- Problem statement
- Quickly run
-
πΊ
long
variable read invalid value- Demo description
- Problem statement
- Quickly run
-
πΊ the result of concurrency count without synchronization is wrong
- Demo description
- Problem statement
- Quickly run
-
πΊ Synchronization on mutable fields
- Demo description
- Problem statement
- Quickly run
-
πΊ Deadlock caused by the symmetric locks
- Demo description
- Problem statement
- Quickly run
πΊ Update without synchronization cannot be read in another thread
Demo class com.oldratlee.fucking.concurrency.NoPublishDemo
.
Demo description
Set the field stop
to true
in the main
thread to control the exit of the task thread started in main
.
Problem statement
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.
Quickly run
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.NoPublishDemo
πΊ Infinite loop of HashMap
This problem has been explained in many places.
The Demo class com.oldratlee.fucking.concurrency.HashMapHangDemo
can reproduce this problem.
Demo description
Two task threads are opened in the main thread to perform the put operation of HashMap
. The main thread does the get operation.
Problem statement
The main thread Block is determined by no continuous output, that is, the endless loop of HashMap
appears.
Quickly run
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.HashMapHangDemo
πΊ Combined state read invalid combination
When programming, multiple state records will be required (state can be a POJO
object or int
s, etc.).
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.
Invalid combinations are combinations that have never been set.
Demo description
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. Demo class com.oldratlee.fucking.concurrency.InvalidCombinationStateDemo.
Problem statement
The second state read in the task thread is not twice the value of the first state, that is, an invalid value.
Quickly run
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.InvalidCombinationStateDemo
πΊ long
variable read invalid value
An invalid value is a value that has never been set.
Reading and writing of long
variables is not atomic and will be divided into two 4-byte operations.
Demo class com.oldratlee.fucking.concurrency.InvalidLongDemo
.
Demo description
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.
Problem statement
In the task thread, a long variable whose upper 4 bytes and lower 4 bytes are different is read, which is an invalid value.
Quickly run
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.InvalidLongDemo
πΊ the result of concurrency count without synchronization is wrong
Demo class com.oldratlee.fucking.concurrency.WrongCounterDemo
.
Demo description
Two task threads are opened in the main thread to execute concurrent incrementing counts. Main thread final result check.
Problem statement
The count value is incorrect.
Quickly run
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.WrongCounterDemo
πΊ Synchronization on mutable fields
It is common to see synchronization code on a volatile field, and the person who write it will naturally feel that this is safe and correct.
# For problem analysis, see the article Synchronization on mutable fields.
Demo class com.oldratlee.fucking.concurrency.SynchronizationOnMutableFieldDemo
.
Demo description
Two task threads are opened in the main thread to execute addListener
. Main thread final result check.
Problem statement
The final count of Listeners is incorrect.
Quickly run
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.SynchronizationOnMutableFieldDemo
πΊ Deadlock caused by the symmetric locks
# For problem analysis, see the article Synchronization on mutable fields
Demo class com.oldratlee.fucking.concurrency.SymmetricLockDeadlockDemo
.
Demo description
Two task threads are opened in the main thread for execution.
Problem statement
Task thread deadlocked.
Quickly run
mvn compile exec:java -Dexec.mainClass=com.oldratlee.fucking.concurrency.SymmetricLockDeadlockDemo