ποΈ 27082025 2330
π
java_concurrency_guidelines
Impact Levelsβ
Impact Level | Meaning | Examples |
---|---|---|
π¨ Critical | Must fix immediately; could cause severe outages, data loss, or avalanches | Unbounded queues, inconsistent locks, ThreadLocal leaks |
β οΈ High | Should fix promptly; could affect stability or performance | Missing Future timeouts, bad collection choice |
π’ Advisory | Best practice; improves maintainability and efficiency | Thread naming, Random vs ThreadLocalRandom |
π§΅ Thread Managementβ
Guideline | Impact | Why It Matters |
---|---|---|
Limit total threads to β€ min(5000, 200 * CPU) | β οΈ High | Too many threads β context switching overhead + system jitter |
Avoid parallel streams for I/O | π¨ Critical | Parallel streams share the ForkJoinPool β I/O blocks all tasks, harming latency |
Clean up ThreadLocal variables after use | π¨ Critical | Thread pools reuse threads β stale data leaks + unexpected cross-request contamination |
Always handle CompletableFuture exceptions | β οΈ High | Uncaught exceptions are silently dropped β makes debugging difficult |
Use custom thread pools for CompletableFuture | π¨ Critical | Default pool = small, shared β risk of starvation or excessive thread creation |
Set timeouts on Future.get() | β οΈ High | Without timeouts, a blocked future can hang threads indefinitely |
πββοΈ Thread Pool Best Practicesβ
Guideline | Impact | Why It Matters |
---|---|---|
Core threads β max threads | π’ Advisory | Avoids frequent creation/destruction > better latency and CPU usage |
Keep-alive β₯ 1 min | β οΈ High if < 30s, π¨ Critical if < 10s | Short lifetimes thrash thread creation, increasing CPU spikes |
Avoid unbounded queues | π¨ Critical | Tasks pile up > unbounded memory growth > OOM risk |
Specify custom pools for @Async | π¨ Critical | Default Spring async pool is unbounded > uncontrolled thread growth |
Specify custom pools for WebAsyncTask / DeferredResult | π¨ Critical | Defaults to shared pool > contention under high traffic |
Name threads meaningfully | π’ Advisory | Helps debugging via jstack and log tracing |
π‘οΈ Thread Safety Guidelinesβ
Guideline | Impact | Why It Matters |
---|---|---|
Use proper double-checked locking | π¨ Critical | Incorrect ordering β partially initialized objects β undefined behavior |
Donβt start threads in constructors | β οΈ High | Threads may run before subclass initialization β inconsistent state |
Donβt synchronize on value types (String , Integer , LocalDateTime ) | π¨ Critical | Value types are cached internally β accidental cross-object lock sharing |
Prefer DateTimeFormatter over SimpleDateFormat | π’ Advisory | SimpleDateFormat is not thread-safe; race conditions likely |
Keep lock acquisition order consistent | π¨ Critical | Inconsistent ordering β deadlocks under load |
π Concurrent Performance Optimizationβ
Guideline | Impact | Why It Matters |
---|---|---|
Use ConcurrentHashMap instead of HashMap + synchronized | β οΈ High | Fine-grained locking in ConcurrentHashMap β better throughput |
Minimize lock scope | β οΈ High | Smaller critical sections β higher concurrency, less blocking |
Use ThreadLocalRandom instead of Random | π’ Advisory | Avoids contention on shared Random state |
Use LongAdder instead of AtomicLong for hot counters | π’ Advisory | Better under high contention due to striped counters |
π Quick Prioritization Tableβ
Area | Must-Fix π¨ | Should-Fix β οΈ | Best Practice π’ |
---|---|---|---|
Threads | Parallel I/O streams, ThreadLocal leaks, custom pool for CFutures | Thread limits, Future timeouts | Thread naming |
Pools | Unbounded queues, default @Async , default WebAsyncTask pools | Keep-alive tuning | Core=max threads |
Safety | Lock order consistency, bad value locks, bad DCL patterns | Starting threads in constructors | Use DateTimeFormatter |
Performance | β | ConcurrentHashMap, lock minimization | LongAdder, ThreadLocalRandom |
Key Insightsβ
- The Critical rules are mostly about preventing outages (e.g., OOM, deadlocks, cascading failures).
- The High rules aim at avoiding silent performance killers (e.g., starvation, slow I/O).
- The Advisory rules improve debuggability, maintainability, and long-term scalability.