Cursor-rules-java 125-java-concurrency
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. Part of the skills-for-java project
git clone https://github.com/jabrena/cursor-rules-java
T=$(mktemp -d) && git clone --depth=1 https://github.com/jabrena/cursor-rules-java "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/125-java-concurrency" ~/.claude/skills/jabrena-cursor-rules-java-125-java-concurrency && rm -rf "$T"
skills/125-java-concurrency/SKILL.mdJava 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
, Java Memory ModelReadWriteLock
thread pool configuration: sizing, keep-alive, bounded queues, rejection policies, graceful shutdownExecutorService- 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 scalabilityExecutors.newVirtualThreadPerTaskExecutor()
overScopedValue
for immutable cross-task dataThreadLocal- Cooperative cancellation and
disciplineInterruptedException - Backpressure with bounded queues and
CallerRunsPolicy - Deadlock avoidance via global lock ordering and
with timeoutstryLock - ForkJoin/parallel-stream discipline for CPU-bound work
- Virtual-thread pinning detection (JFR
)VirtualThreadPinned - Thread naming and
observabilityUncaughtExceptionHandler - 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.
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 compile
before 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 verify
after applying improvementsmvn clean verify - 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
Reference
For detailed guidance, examples, and constraints, see references/125-java-concurrency.md.