125-java-concurrency
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJava rules for Concurrency objects
Java并发对象规则
Identify and apply Java concurrency best practices to improve thread safety, scalability, and maintainability by using modern utilities and virtual threads.
java.util.concurrentWhat is covered in this Skill?
- Thread safety fundamentals: ,
ConcurrentHashMap,AtomicInteger,ReentrantLock, Java Memory ModelReadWriteLock - thread pool configuration: sizing, keep-alive, bounded queues, rejection policies, graceful shutdown
ExecutorService - Producer-Consumer and Publish-Subscribe patterns with
BlockingQueue - for non-blocking async composition (
CompletableFuture/thenApply/thenCompose/exceptionally)orTimeout - Immutability and safe publication (, static initializers)
volatile - Lock contention and false-sharing performance optimization
- Virtual threads () for I/O-bound scalability
Executors.newVirtualThreadPerTaskExecutor() - over
ScopedValuefor immutable cross-task dataThreadLocal - Cooperative cancellation and discipline
InterruptedException - Backpressure with bounded queues and
CallerRunsPolicy - Deadlock avoidance via global lock ordering and with timeouts
tryLock - ForkJoin/parallel-stream discipline for CPU-bound work
- Virtual-thread pinning detection (JFR )
VirtualThreadPinned - Thread naming and observability
UncaughtExceptionHandler - Fit-for-purpose primitives: ,
LongAdder,CopyOnWriteArrayList,StampedLock,Semaphore,CountDownLatchPhaser
Scope: The reference is organized by examples (good/bad code patterns) for each core area. Apply recommendations based on applicable examples.
通过使用现代工具类和虚拟线程,识别并应用Java并发最佳实践,以提升线程安全性、可扩展性和可维护性。
java.util.concurrent本技能涵盖哪些内容?
- 线程安全基础:、
ConcurrentHashMap、AtomicInteger、ReentrantLock、Java内存模型ReadWriteLock - 线程池配置:大小设置、存活时间、有界队列、拒绝策略、优雅关闭
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、CountDownLatchPhaser
范围:参考文档按每个核心领域的示例(良好/不良代码模式)组织。根据适用示例应用建议。
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 or
./mvnw compilebefore applying any changemvn compile - SAFETY: If compilation fails, stop immediately — compilation failure is a blocking condition that prevents any further processing
- VERIFY: Run or
./mvnw clean verifyafter applying improvementsmvn clean verify - BEFORE APPLYING: Read the reference for detailed good/bad examples, constraints, and safeguards for each concurrency pattern
在应用任何并发修改之前,确保项目能够编译。如果编译失败,请立即停止——编译失败是阻塞条件。应用改进后,运行完整验证。
- 强制要求:在应用任何修改前运行或
./mvnw compilemvn compile - 安全规则:如果编译失败,请立即停止——编译失败是阻塞条件,阻止任何后续处理
- 验证步骤:应用改进后运行或
./mvnw clean verifymvn clean verify - 应用前须知:阅读参考文档,了解每个并发模式的详细良好/不良示例、约束条件和防护措施
When to use this skill
何时使用本技能
- Review Java code for concurrency
- 审查Java代码的并发问题
Workflow
工作流程
- Compile project before concurrency changes
Run or and stop immediately if compilation fails.
./mvnw compilemvn compile- Read concurrency reference and analyze hotspots
Read and identify thread-safety, coordination, and throughput issues to address.
references/125-java-concurrency.md- Apply concurrency improvements
Implement suitable concurrency patterns, cancellation discipline, and fit-for-purpose primitives.
- Verify with full build
Run or after applying improvements.
./mvnw clean verifymvn clean verify- 在并发修改前编译项目
运行或,如果编译失败则立即停止。
./mvnw compilemvn compile- 阅读并发参考文档并分析热点问题
阅读,识别需要解决的线程安全、协调和吞吐量问题。
references/125-java-concurrency.md- 应用并发改进方案
实现合适的并发模式、取消规范以及适用场景匹配的原语。
- 通过完整构建验证
应用改进后运行或。
./mvnw clean verifymvn clean verifyReference
参考文档
For detailed guidance, examples, and constraints, see references/125-java-concurrency.md.
如需详细指导、示例和约束条件,请查看references/125-java-concurrency.md。