GitRead 中文社区
首页趋势排行榜原创文章严选周刊
更多资源
新手训练营
精选专题热门话题GitHub 文档资源精选实战指南

fucking-java-concurrency

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

  • 语言Java
  • 默认分支master
  • 大小165 KB
  • 更新1 周前
  • Issues2
在 GitHub 打开
avataroldratlee

项目介绍

oldratlee/fucking-java-concurrency 是一个托管在 GitHub 上的开源项目,主要使用 Java 编写,采用 Apache License 2.0 许可证,目前获得 1.2k 个 Star、343 次 Fork。

仓库全名
oldratlee/fucking-java-concurrency

README 摘要

🎏 fucking-java-concurrency 📖 English Documentation | 📖 中文文档 Minimal demos of Java concurrency problems — seeing 🙈 is believing 🐵. 🍎 Why these demos An observed symptom 🙈 is more intuitive and convincing than a stated concurrency principle 🙊. The Java standard library supports threads, and multithreading is heavily used both in the language itself (e.g. GC) and in applications (server side). Concurrency…

完整内容见 GitHub 原始 README 或本页 。

常见问题

fucking-java-concurrency 用什么语言开发?

主要使用 Java 编写,采用 Apache License 2.0 许可证,目前有 1.2k 个 Star。

fucking-java-concurrency 可以免费商用吗?

仓库采用 Apache License 2.0 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 fucking-java-concurrency 的源码?

可以直接克隆仓库:git clone https://github.com/oldratlee/fucking-java-concurrency.git,默认分支为 master;安装与运行步骤见仓库 README。

fucking-java-concurrency 最近还在维护吗?

仓库最近一次更新为 1 周前,当前有 2 个未关闭 Issue;Star 数为 1.2k,可作为社区活跃度的参考。

本页面展示项目元数据与原始 README,GitRead 中文社区 编辑团队负责选题收录。

相关仓库

GrowingGit/GitHub-Chinese-Top-Charts avatar
GitHub-Chinese-Top-ChartsGrowingGit
2024年10月12日
108.6k
13.5k
193

:cn: GitHub中文排行榜,各语言分设「软件 | 资料」榜单,精准定位中文好项目。各取所需,高效学习。

Java
120.3 MB
1
详情
NationalSecurityAgency/ghidra avatar
ghidraNationalSecurityAgency
4 小时前
99.3k
11.1k
1.6k

Ghidra 是一个软件逆向工程(SRE)框架

software-analysisdisassemblerreverse-engineering
Java
408.2 MB
0
详情
iluwatar/java-design-patterns avatar
java-design-patternsiluwatar
10月8日
94.9k
27.4k
146

用 Java 实现的设计模式

javaprinciplesdesign-patterns+5
Java
47.6 MB
1
详情
macrozheng/mall avatar
mallmacrozheng
9月15日
85.0k
29.5k
11

mall项目是一套电商系统,包括前台商城系统及后台管理系统,基于Spring Boot+MyBatis实现,采用Docker容器化部署。 前台商城系统包含首页门户、商品推荐、商品搜索、商品展示、购物车、订单流程、会员中心、客户服务、帮助中心等模块。 后台管理系统包含商品管理、订单管理、会员管理、促销管理、运营管理、内容管理、统计报表、财务管理、权限管理、设置等模块。

spring-bootspring-securitymybatis+7
Java
57.4 MB
0
详情
spring-projects/spring-boot avatar
spring-bootspring-projects
1 小时前
81.8k
40.9k
527

Spring Boot 帮助你以最少的繁琐工作创建由 Spring 驱动的、生产级应用和服务。

frameworkjavaspring+1
Java
221.3 MB
0
详情
doocs/advanced-java avatar
advanced-javadoocs
3 周前
79.2k
19.1k
1

资深 Java(后端)开发人员核心面试题与答案 | 互联网 Java 工程师进阶知识完全扫盲:涵盖高并发、分布式、高可用、微服务、海量数据处理等领域知识

javadistributed-systemsmessage-queue+7
Java
33.7 MB
0
详情
elastic/elasticsearch avatar
elasticsearchelastic
4 小时前
78.5k
25.8k
4.9k

免费开源、分布式、RESTful 搜索引擎

elasticsearchjavasearch-engine
Java
1.8 GB
0
详情
MisterBooo/LeetCodeAnimation avatar
LeetCodeAnimationMisterBooo
6月12日
76.7k
13.9k
23

以动画形式演示 LeetCode 上的所有题目。(用动画的形式呈现解 LeetCode 题目的思路,完整单步/回看/变速/语音讲解在 algomooc.com)

animationleetcodeleetcode-c+2
Java
498.4 MB
0
详情

关于

concurrencyconcurrency-gotchasconcurrency-issueconcurrency-problemconcurrent-programmingdeadlockdemojavamulti-threadingmultithreadingparallelismshowcasesyncsynchronizationthread-safety
  • Apache License 2.0
  • 1.2k Stars
  • 343 Forks
  • 57 Watching

简要分析

热度分
1958
价值分
837
活跃状态
活跃
GitHub

语言占比

Java

贡献者

oldratleeoldratleedependabot[bot]dependabot[bot]beatrichartzbeatrichartzmonadromemonadromeWeiXiao-HyyWeiXiao-Hyy

🎏 fucking-java-concurrency

Github Workflow Build Status Java support License GitHub Stars GitHub Forks GitHub Contributors GitHub repo size gitpod: Ready to Code


📖 English Documentation | 📖 中文文档

Minimal demos of Java concurrency problems — seeing 🙈 is believing 🐵.

🍎 Why these demos

  • An observed symptom 🙈 is more intuitive and convincing than a stated concurrency principle 🙊.
  • The Java standard library supports threads, and multithreading is heavily used both in the language itself (e.g. GC) and in applications (server side).
  • Concurrency greatly increases the complexity of program design, analysis, and implementation. Unless you fully understand and systematically analyze the concurrent logic, writing code at random means it is no exaggeration to say that such a program runs correctly by accident.
    • The demos here come with no explanation or discussion, and all of them are entry-level 🤓 . For more information, look up concurrency material yourself.

Hit a concurrency problem in your own development? Please share it — submit an issue or fork the repo and open a pull request! 😘


  • 🍺 Unsynchronized updates are not visible to other threads
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Infinite loop in HashMap
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Reading combined state yields an invalid combination
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Reading a long variable yields an invalid value
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Consecutive reads of the same field see different values
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Unsynchronized concurrent counting gives wrong results
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Synchronization on mutable fields
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Deadlock caused by symmetric locks
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Livelock caused by reentrant locks
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Instruction reordering causes incorrect reads of non-final fields
    • Demo description
    • Problem statement
    • Quick run
  • 🍺 Cyclic thread pool deadlock
    • Demo description
    • Problem statement
    • Quick run

🍺 Unsynchronized updates are not visible to other threads

Demo class NoPublishDemo.

Demo description

The main thread sets the field stop to true to signal the task thread (started in main) to exit.

Problem statement

After the main thread sets stop to true, the task thread keeps running — i.e. it never sees the new value.

Quick run

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

🍺 Infinite loop in HashMap

This problem has been explained in many places.

The Demo class HashMapHangDemo can reproduce this problem.

Demo description

The main thread starts two task threads that put into the HashMap, while itself repeatedly performing get.

Problem statement

The main thread blocks (output stops), i.e. the HashMap has entered an infinite loop.

Quick run

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

🍺 Reading combined state yields an invalid combination

Programs often need to track several related pieces of state (a POJO, a few ints, etc.).

Multi-state read/write code is frequently left unsynchronized, and whoever writes it naturally overlooks thread safety.

An invalid combination is a combination of values that the program never actually wrote.

Demo description

The main thread modifies multiple states. For convenience of checking, every write keeps a fixed relationship: the second state is always twice the first. A task thread reads the states. Demo class InvalidCombinationStateDemo.

Problem statement

The task thread reads a second state that is not twice the first — a combination that was never written.

Quick run

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

🍺 Reading a long variable yields an invalid value

An invalid value is one that was never written.

Reads and writes of non-volatile long and double are not guaranteed to be atomic.

per the Java Language Specification (§17.7), a single read/write of a 64-bit value may be treated as two separate 32-bit accesses. On 32-bit JVMs this is necessarily split into two 4-byte operations; on 64-bit JVMs it is typically done atomically in practice, but the specification still does not guarantee atomicity.

Demo class InvalidLongDemo.

Demo description

The main thread modifies the long variable. For convenience of checking, the upper and lower 4 bytes of every value written are identical. A task thread reads the long.

Problem statement

The task thread reads a long whose upper and lower 4 bytes differ — a value that was never written.

Quick run

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

🍺 Consecutive reads of the same field see different values

Demo class InconsistentReadDemo.

Demo description

The main thread increments the plain count field in a tight loop. A task thread performs two consecutive reads of count and reports each time the two values differ.

Problem statement

Two consecutive reads of the same non-volatile field by the same thread return different values. For a plain field, each read may observe a different write, so a thread must not assume that two reads see the same value.

Quick run

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

🍺 Unsynchronized concurrent counting gives wrong results

Demo class WrongCounterDemo.

Demo description

The main thread starts two task threads that increment a shared counter concurrently, then checks the final result.

Problem statement

The final count is incorrect.

Quick run

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

🍺 Synchronization on mutable fields

Synchronizing on a volatile field is common, and whoever writes it naturally assumes this is safe and correct.
# For problem analysis, see the article Synchronization on mutable fields.

Demo class SynchronizationOnMutableFieldDemo.

Demo description

The main thread starts two task threads that call addListener, then checks the final result.

Problem statement

The final listener count is incorrect.

Quick run

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

🍺 Deadlock caused by symmetric locks

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

Demo class SymmetricLockDeadlockDemo.

Demo description

The main thread starts two task threads, each of which takes the two locks in the opposite order from the other.

Problem statement

The task threads deadlock.

Quick 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 the article

Demo class ReentrantLockLivelockDemo.

Demo description

Two task threads try to acquire the lock the other thread holds while holding their own lock.

Problem statement

The threads release their own lock and immediately re-acquire it, denying the other thread a chance to acquire both locks. The threads keep executing: an attempt wastes its work, and overall they make almost no actual progress. This is a livelock.

Quick run

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

🍺 Instruction reordering causes incorrect reads of non-final fields

Demo class FinalInitialDemo.

Demo description

The writer thread calls the class constructor while the reader thread reads the class’s non-final fields.

Problem statement

When the constructor runs, instruction reordering can move the stores of non-final fields outside it, so the reader thread may observe the fields’ default values instead of the values set by the constructor. (Reordering is not guaranteed; it requires specific hardware and JVM conditions.)

Quick run

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

🍺 Cyclic thread pool deadlock

Demo class CyclicThreadPoolDeadLockDemo.

Demo description

This example demonstrates the deadlock caused by cyclic dependencies between tasks when using thread pools, and how to avoid it with CompletableFuture.

Problem statement

In the badCase, two thread pools, pool1 and pool2, submit tasks to each other, forming a cyclic dependency. When the pools’ threads are exhausted, every running task waits for another task to complete, and the pools deadlock. The goodCase replaces this with asynchronous chained calls using CompletableFuture, so no pool thread blocks.

Quick run

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

评论

登录 后发表评论
暂无评论
GitRead 中文社区
深度解读 · 趋势洞察 · 价值精选

关于

关于我们联系我们编辑准则收录标准赞赏人员

发现

技术趋势新手训练营资源精选实战指南精选专题热门话题GitHub 文档

条款

隐私政策服务条款网站地图
26.9万收录项目
5.8万中文话题
155精选专题
发现技术价值,连接开源智慧
© 2026 GitRead · GitHub 中文社区 本站与 GitHub, Inc. 无隶属关系;GitHub 是 GitHub, Inc. 的商标。