25

Resource and concurrency patterns: pools, monitors, active objects

A bounded connection pool, a monitor object, and an active object, reconsidered under virtual threads.

Demo 25Source on GitHub ↗

Real-world concurrency patterns for scarce resources. This module builds a bounded CarrierConnectionPool (a Semaphore plus a free list, a blocking timed acquire, AutoCloseable leases for try-with-resources), a DispatchDock monitor object that serializes concurrent access with ReentrantLock and a Condition, and a LabelPrintActiveObject that decouples a method call from the worker thread that runs it. It also shows the ThreadLocal cost and defensive immutable snapshots. The point under virtual threads: pool the scarce resource, not the threads.

The JUnit 5 tests are deterministic (latches and futures, no sleeps): the pool never exceeds its bound and reuses instances, the monitor holds its invariant under concurrent callers, and the active object processes queued calls off the caller thread.

Build, run, observe

cd examples/25-resource-concurrency
mvn -q test
./demo.sh

The module also runs from the aggregator root with mvn -q -f examples/pom.xml test.