125-java-concurrency

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Java rules for Concurrency objects

Java并发对象规则

Identify and apply Java concurrency best practices to improve thread safety, scalability, and maintainability by using modern
java.util.concurrent
utilities and virtual threads.
What is covered in this Skill?
  • Thread safety fundamentals:
    ConcurrentHashMap
    ,
    AtomicInteger
    ,
    ReentrantLock
    ,
    ReadWriteLock
    , Java Memory Model
  • ExecutorService
    thread pool configuration: sizing, keep-alive, bounded queues, rejection policies, graceful shutdown
  • Producer-Consumer and Publish-Subscribe patterns with
    BlockingQueue
  • CompletableFuture
    for non-blocking async composition (
    thenApply
    /
    thenCompose
    /
    exceptionally
    /
    orTimeout
    )
  • Immutability and safe publication (
    volatile
    , static initializers)
  • Lock contention and false-sharing performance optimization
  • Virtual threads (
    Executors.newVirtualThreadPerTaskExecutor()
    ) for I/O-bound scalability
  • ScopedValue
    over
    ThreadLocal
    for immutable cross-task data
  • Cooperative cancellation and
    InterruptedException
    discipline
  • Backpressure with bounded queues and
    CallerRunsPolicy
  • Deadlock avoidance via global lock ordering and
    tryLock
    with timeouts
  • ForkJoin/parallel-stream discipline for CPU-bound work
  • Virtual-thread pinning detection (JFR
    VirtualThreadPinned
    )
  • Thread naming and
    UncaughtExceptionHandler
    observability
  • Fit-for-purpose primitives:
    LongAdder
    ,
    CopyOnWriteArrayList
    ,
    StampedLock
    ,
    Semaphore
    ,
    CountDownLatch
    ,
    Phaser
Scope: The reference is organized by examples (good/bad code patterns) for each core area. Apply recommendations based on applicable examples.
通过使用现代
java.util.concurrent
工具类和虚拟线程,识别并应用Java并发最佳实践,以提升线程安全性、可扩展性和可维护性。
本技能涵盖哪些内容?
  • 线程安全基础:
    ConcurrentHashMap
    AtomicInteger
    ReentrantLock
    ReadWriteLock
    、Java内存模型
  • ExecutorService
    线程池配置:大小设置、存活时间、有界队列、拒绝策略、优雅关闭
  • 基于
    BlockingQueue
    的生产者-消费者与发布-订阅模式
  • 用于非阻塞异步组合的
    CompletableFuture
    thenApply
    /
    thenCompose
    /
    exceptionally
    /
    orTimeout
  • 不可变性与安全发布(
    volatile
    、静态初始化器)
  • 锁竞争与伪共享性能优化
  • 用于I/O密集型场景可扩展性的虚拟线程(
    Executors.newVirtualThreadPerTaskExecutor()
  • 替代
    ThreadLocal
    ScopedValue
    用于不可变跨任务数据传递
  • 协作式取消与
    InterruptedException
    规范
  • 基于有界队列和
    CallerRunsPolicy
    的背压机制
  • 通过全局锁排序和带超时的
    tryLock
    避免死锁
  • 适用于CPU密集型任务的ForkJoin/并行流规范
  • 虚拟线程固定检测(JFR
    VirtualThreadPinned
  • 线程命名与
    UncaughtExceptionHandler
    可观测性
  • 适用场景匹配的原语:
    LongAdder
    CopyOnWriteArrayList
    StampedLock
    Semaphore
    CountDownLatch
    Phaser
范围:参考文档按每个核心领域的示例(良好/不良代码模式)组织。根据适用示例应用建议。

Constraints

约束条件

Before applying any concurrency changes, ensure the project compiles. If compilation fails, stop immediately — compilation failure is a blocking condition. After applying improvements, run full verification.
  • MANDATORY: Run
    ./mvnw compile
    or
    mvn compile
    before applying any change
  • SAFETY: If compilation fails, stop immediately — compilation failure is a blocking condition that prevents any further processing
  • VERIFY: Run
    ./mvnw clean verify
    or
    mvn clean verify
    after applying improvements
  • BEFORE APPLYING: Read the reference for detailed good/bad examples, constraints, and safeguards for each concurrency pattern
在应用任何并发修改之前,确保项目能够编译。如果编译失败,请立即停止——编译失败是阻塞条件。应用改进后,运行完整验证。
  • 强制要求:在应用任何修改前运行
    ./mvnw compile
    mvn compile
  • 安全规则:如果编译失败,请立即停止——编译失败是阻塞条件,阻止任何后续处理
  • 验证步骤:应用改进后运行
    ./mvnw clean verify
    mvn clean verify
  • 应用前须知:阅读参考文档,了解每个并发模式的详细良好/不良示例、约束条件和防护措施

When to use this skill

何时使用本技能

  • Review Java code for concurrency
  • 审查Java代码的并发问题

Workflow

工作流程

  1. Compile project before concurrency changes
Run
./mvnw compile
or
mvn compile
and stop immediately if compilation fails.
  1. Read concurrency reference and analyze hotspots
Read
references/125-java-concurrency.md
and identify thread-safety, coordination, and throughput issues to address.
  1. Apply concurrency improvements
Implement suitable concurrency patterns, cancellation discipline, and fit-for-purpose primitives.
  1. Verify with full build
Run
./mvnw clean verify
or
mvn clean verify
after applying improvements.
  1. 在并发修改前编译项目
运行
./mvnw compile
mvn compile
,如果编译失败则立即停止。
  1. 阅读并发参考文档并分析热点问题
阅读
references/125-java-concurrency.md
,识别需要解决的线程安全、协调和吞吐量问题。
  1. 应用并发改进方案
实现合适的并发模式、取消规范以及适用场景匹配的原语。
  1. 通过完整构建验证
应用改进后运行
./mvnw clean verify
mvn clean verify

Reference

参考文档

For detailed guidance, examples, and constraints, see references/125-java-concurrency.md.
如需详细指导、示例和约束条件,请查看references/125-java-concurrency.md