concurrency-interview: companion code for the Top 40 interview post
Adds the concurrency-interview module: a real race condition with its actual lost-update count, why volatile alone doesn't fix it, a real JVM-detected deadlock (ThreadMXBean.findDeadlockedThreads()) with the fix, and ScopedValue's exact child-thread inheritance rules (finalized in JDK 25 via JEP 506, demonstrated against StructuredTaskScope, still preview per JEP 505/525). 4 runnable demos, 1 JUnit test class (9 tests), 5 captured output transcripts. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FhzLY5p6okFva3qsnsRyvM
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
expected increments: 1600000
|
||||
unsafeCounter (plain int, unsynchronized): 1248496
|
||||
safeCounter (AtomicInteger): 1600000
|
||||
unsafeCounter lost 351504 updates to the race
|
||||
safeCounter matches expected: true
|
||||
@@ -0,0 +1,5 @@
|
||||
expected increments: 1600000
|
||||
volatileCounter: 855018 (volatile guarantees every thread SEES the latest value - it does not make ++ atomic)
|
||||
atomicCounter: 1600000
|
||||
volatileCounter lost updates: 744982
|
||||
atomicCounter matches expected: true
|
||||
@@ -0,0 +1,12 @@
|
||||
=== Opposite lock order: a real deadlock, detected via ThreadMXBean ===
|
||||
thread-1: holding lockA, waiting for lockB
|
||||
thread-2: holding lockB, waiting for lockA
|
||||
main: findDeadlockedThreads() detected 2 deadlocked threads
|
||||
thread-1 is blocked on java.lang.Object@5451c3a8, owned by thread-2
|
||||
thread-2 is blocked on java.lang.Object@49476842, owned by thread-1
|
||||
|
||||
=== Same lock order on both threads: no deadlock possible ===
|
||||
thread-1 (fixed order): acquired both locks, done
|
||||
thread-2 (fixed order): acquired both locks, done
|
||||
main: both fixed-order threads finished, isAlive t1=false t2=false
|
||||
done (the process exits here - the two deadlocked threads above never finish)
|
||||
@@ -0,0 +1,13 @@
|
||||
=== Same-thread binding: visible to direct AND indirect callees ===
|
||||
handleRequest(): REQUEST_ID.get() = req-42
|
||||
logSomewhereDeeper() (indirect callee, same thread): REQUEST_ID.get() = req-42
|
||||
|
||||
=== Outside any binding: isBound() is false ===
|
||||
main: REQUEST_ID.isBound() = false
|
||||
|
||||
=== A plain `new Thread()` does NOT inherit the binding ===
|
||||
plain child thread: REQUEST_ID.isBound() = false
|
||||
|
||||
=== A StructuredTaskScope.fork()'d thread DOES inherit the binding ===
|
||||
forked subtask: REQUEST_ID.isBound() = true, REQUEST_ID.get() = req-100
|
||||
done
|
||||
@@ -0,0 +1,4 @@
|
||||
-------------------------------------------------------------------------------
|
||||
Test set: com.ankurm.concurrencyinterview.InterviewClaimsTest
|
||||
-------------------------------------------------------------------------------
|
||||
Tests run: 9, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.216 s -- in com.ankurm.concurrencyinterview.InterviewClaimsTest
|
||||
Reference in New Issue
Block a user