Concurrency, visibility and virtual threads
Prerequisites: 01-ecosystem
Where should concurrency be limited?
Goal & mental model verified
Concurrency coordinates overlapping tasks. Visibility and atomicity are different: volatile can publish writes, but does not make read-modify-write increments atomic. Virtual threads make blocking I/O concurrency cheaper; they do not create CPU or database capacity.
[S11] [S12]Worked example · design exercise synthesis
Two workers read count=0 and both write 1: one increment is lost. AtomicInteger or a lock can protect this invariant. For quote calls, a semaphore can bound access to a scarce insurer connection.
[S11] [S12] [S13]Engineering decision synthesis
Use virtual threads for many blocking I/O tasks when dependencies fit. Bound downstream concurrency; use workload-appropriate executors for CPU work and decide timeout/cancellation ownership.
[S11] [S12] [S13]Pitfall & diagnosis synthesis
A shared mutable singleton remains shared under virtual threads. Avoid pooling virtual threads. Pinning guidance is JDK-dependent; older synchronized pinning advice should not be repeated universally for newer JDKs.
[S11] [S12] [S13]Improve & validate synthesis
Load-test with slow dependencies. Record pool wait, in-flight calls, CPU and tail latency; stop increasing concurrency when the constrained resource saturates.
[S11] [S12] [S13]Check yourself: Does volatile count++ become atomic?
No. Reading, adding and writing remain a compound operation.