Java rules for Concurrency objects
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.
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
When to use this skill
- Review Java code for concurrency
Workflow
- Compile project before concurrency changes
Run ./mvnw compile or mvn compile and stop immediately if compilation fails.
- Read concurrency reference and analyze hotspots
Read references/125-java-concurrency.md and identify thread-safety, coordination, and throughput issues to address.
- Apply concurrency improvements
Implement suitable concurrency patterns, cancellation discipline, and fit-for-purpose primitives.
- Verify with full build
Run ./mvnw clean verify or mvn clean verify after applying improvements.
Reference
For detailed guidance, examples, and constraints, see references/125-java-concurrency.md.
1---2name: 125-java-concurrency3description: Use when you need to apply Java concurrency best practices — including thread safety fundamentals, ExecutorService thread pool management, concurrent design patterns like Producer-Consumer, asynchronous programming with CompletableFuture, immutability and safe publication, deadlock avoidance, virtual threads, scoped values, backpressure, cancellation discipline, and observability for concurrent systems. This should trigger for requests such as Review Java code for concurrency. Part of cursor-rules-java project4license: Apache-2.05---6# Java rules for Concurrency objects
7
8Identify and apply Java concurrency best practices to improve thread safety, scalability, and maintainability by using modern `java.util.concurrent` utilities and virtual threads.
9
10**What is covered in this Skill?**
11
12- Thread safety fundamentals: `ConcurrentHashMap`, `AtomicInteger`, `ReentrantLock`, `ReadWriteLock`, Java Memory Model
13- `ExecutorService` thread pool configuration: sizing, keep-alive, bounded queues, rejection policies, graceful shutdown
14- Producer-Consumer and Publish-Subscribe patterns with `BlockingQueue`
15- `CompletableFuture` for non-blocking async composition (`thenApply`/`thenCompose`/`exceptionally`/`orTimeout`)
16- Immutability and safe publication (`volatile`, static initializers)
17- Lock contention and false-sharing performance optimization
18- Virtual threads (`Executors.newVirtualThreadPerTaskExecutor()`) for I/O-bound scalability
19- `ScopedValue` over `ThreadLocal` for immutable cross-task data
20- Cooperative cancellation and `InterruptedException` discipline
21- Backpressure with bounded queues and `CallerRunsPolicy`
22- Deadlock avoidance via global lock ordering and `tryLock` with timeouts
23- ForkJoin/parallel-stream discipline for CPU-bound work
24- Virtual-thread pinning detection (JFR `VirtualThreadPinned`)
25- Thread naming and `UncaughtExceptionHandler` observability
26- Fit-for-purpose primitives: `LongAdder`, `CopyOnWriteArrayList`, `StampedLock`, `Semaphore`, `CountDownLatch`, `Phaser`
27
28**Scope:** The reference is organized by examples (good/bad code patterns) for each core area. Apply recommendations based on applicable examples.
29
30## Constraints
31
32Before 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.
33
34- **MANDATORY**: Run `./mvnw compile` or `mvn compile` before applying any change
35- **SAFETY**: If compilation fails, stop immediately — compilation failure is a blocking condition that prevents any further processing
36- **VERIFY**: Run `./mvnw clean verify` or `mvn clean verify` after applying improvements
37- **BEFORE APPLYING**: Read the reference for detailed good/bad examples, constraints, and safeguards for each concurrency pattern
38
39## When to use this skill
40
41- Review Java code for concurrency
42
43## Workflow
44
451. **Compile project before concurrency changes**
46
47Run `./mvnw compile` or `mvn compile` and stop immediately if compilation fails.
48
492. **Read concurrency reference and analyze hotspots**
50
51Read `references/125-java-concurrency.md` and identify thread-safety, coordination, and throughput issues to address.
52
533. **Apply concurrency improvements**
54
55Implement suitable concurrency patterns, cancellation discipline, and fit-for-purpose primitives.
56
574. **Verify with full build**
58
59Run `./mvnw clean verify` or `mvn clean verify` after applying improvements.
60
61## Reference
62
63For detailed guidance, examples, and constraints, see [references/125-java-concurrency.md](references/125-java-concurrency.md).