• Stars
    star
    990
  • Rank 46,251 (Top 1.0 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🎏 Simple showcases of java concurrency problems, seeing 🙈 is believing 🐵

🎏 fucking-java-concurrency

Github Workflow Build Status JDK support License GitHub Stars GitHub Forks GitHub Contributors GitHub repo size


📖 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 as GC) 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 class 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

./mvnw compile exec:java -Dexec.mainClass=fucking.concurrency.demo.NoPublishDemo

🍺 Infinite loop of HashMap

This problem has been explained in many places.

The Demo class 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

./mvnw compile exec:java -Dexec.mainClass=fucking.concurrency.demo.HashMapHangDemo

🍺 Combined state read invalid combination

When programming, multiple state records will be required (state can be a POJO object or ints, 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 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

./mvnw compile exec:java -Dexec.mainClass=fucking.concurrency.demo.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 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

./mvnw compile exec:java -Dexec.mainClass=fucking.concurrency.demo.InvalidLongDemo

🍺 the result of concurrency count without synchronization is wrong

Demo class 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

./mvnw compile exec:java -Dexec.mainClass=fucking.concurrency.demo.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 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

./mvnw compile exec:java -Dexec.mainClass=fucking.concurrency.demo.SynchronizationOnMutableFieldDemo

🍺 Deadlock caused by the symmetric locks

# For problem analysis, see the article Synchronization on mutable fields

Demo class SymmetricLockDeadlockDemo.

Demo description

Two task threads are opened in the main thread for execution.

Problem statement

Task thread deadlocked.

Quickly run

./mvnw compile exec:java -Dexec.mainClass=fucking.concurrency.demo.SymmetricLockDeadlockDemo

🍺 Livelock caused by reentrant locks

# For a problem description, see the paragraph about livelocks in this article

Demo class ReentrantLockLivelockDemo.

Demo description

Two task threads are trying to acquire a lock that the other thread holds while holding their own lock.

Problem statement

While the threads are releasing their own lock constantly, they are also re-locking it immediately, denying the other thread a chance to acquire both locks. Since both threads are not blocked from executing but blocked from doing meaningful work, this is a livelock.

Quickly run

./mvnw compile exec:java -Dexec.mainClass=fucking.concurrency.demo.ReentrantLockLivelockDemo

More Repositories

1

useful-scripts

🐌 useful scripts for making developer's everyday life easier and happier, involved java, shell etc.
Shell
6,927
star
2

translations

🐼 Chinese translations for classic IT resources
6,710
star
3

vim-practice

💘 Vim is awesome! Here is my practice and study log.
Vim Script
555
star
4

reactive-practice-at-taobao

♨️ Reactive @ 淘宝 | Reactive实践、推动、落地的记录与大会分享 | Flow Arch(流式架构)/Reactive Programming(RP/反应式编程)
441
star
5

software-practice-thoughts

📚 🐣 软件实践文集。主题不限,思考讨论有趣有料就好,包含如 系统的模型分析/量化分析、开源漫游者指南、软件可靠性设计实践、平台产品的逻辑与执行… 🥤
Jupyter Notebook
200
star
6

big-data-study

🐳 big data study
147
star
7

land

🍡 Land is a simple Java™ dependency-isolation container via class loader.
Java
134
star
8

hacker-quotes

🎩 An oh-my-zsh plugin, just print a hacker quote randomly when open a terminal.
Shell
89
star
9

log4j2-ttl-thread-context-map

🌳 Log4j2 TTL ThreadContextMap, Log4j2 extension integrated TransmittableThreadLocal to MDC
Java
77
star
10

tcpip-quiz

tcp/ip quiz
63
star
11

io-api

📐 generic API design example by I/O, the demo implementation of https://dzone.com/articles/generic-inputoutput-api-java
Java
50
star
12

java-modern-tech-practice

😎 Java™ modern tech practice sandbox ⏳
Java
46
star
13

Effective-IntelliJ-IDEA

Effective IntelliJ IDEA
Java
36
star
14

statistics-from-data-to-conclusion-4e

《统计学:从数据到结论》第四版 吴喜之老师 的数据文件与代码 | The data and code files of book "statistics: from data to conclusion, 4th edition"
Jupyter Notebook
35
star
15

data-science-practice

数据科学实践 | data science practice
Jupyter Notebook
27
star
16

deep-learning-math

《深度学习的数学》的随书Excel文件
15
star
17

erlang-practice

💟 Erlang is awesome! Here is my Erlang practice and study log.
14
star
18

objc-singleton

How to implement a SAFE Objective-C Singleton
Objective-C
14
star
19

ios-study-materials

Objective C and iOS study materials
14
star
20

oldratlee

whoami / my profile
13
star
21

java-compiler-demo

Demo project of creating dynamic applications with javax.tools.JavaCompiler
Java
13
star
22

my-zsh-settings

😍 An oh-my-zsh plugin for storing my zsh utils and customization separately
Shell
9
star
23

main-runner

wrap java maven project by command line start script
Shell
8
star
24

scala-maven-template-prj

scala maven template project
Scala
7
star
25

leetcode

my submission code of LeetCode problems
Java
7
star
26

kotlin-coroutines-bottom-up

Complete runnable demo code of article "A Bottom-Up View of Kotlin Coroutines"
Kotlin
7
star
27

oldratlee.github.com

👽 my technology blog
CSS
7
star
28

awesome-command-line-env

☕ Awesome Command Line Env
5
star
29

bash-string-escape

java lib for bash string escape
Java
4
star
30

ios-push-notifications-demo

iOS Push Notifications step-by-step demo
Objective-C
4
star
31

shell-matrix

display matrix effect on console by shell
Shell
4
star
32

scala-underneath

scala underneath
Scala
3
star
33

HelloKt

HelloKt 😸, my kotlin playground 🎡
Kotlin
3
star
34

NSJSONTest

测试NSJSONSerialization中Value的类型是否和JSON中声明一致,即类型稳定性
Objective-C
3
star
35

intellij-postfix-templates

my intellij postfix templates
2
star
36

java-tips

the demo code of inner class tips and usage
Java
2
star
37

doulist-wrong-counter-analysis

计数不对的『豆瓣豆列』抓取
Jupyter Notebook
2
star
38

do-btrace

btrace start-up template project
Java
1
star
39

akka-quickstart-scala

akka-quickstart-scala
Batchfile
1
star
40

cats-playground

playground for cats (and other cool scala libs)
Scala
1
star