Commit cbc7650
committed
feat: convert CyclicBarrier subsystem startup demo to use ExecutorService with graceful shutdown
WHAT was added:
- MainWithExecutorService demonstrating subsystem startup coordination using:
- CyclicBarrier to synchronize four subsystems (Web Server, Database, Cache, Messaging Service).
- ExecutorService with a fixed thread pool sized to numberOfSubsystems to manage worker threads.
- Subsystem Runnable that:
- Simulates initialization via Thread.sleep(initializationTimeMillis).
- Calls barrier.await() after initialization so all subsystems wait at the barrier.
- Logs lifecycle events: starting, initialization complete, waiting at barrier, and proceeding after barrier.
- Main performs orderly shutdown:
- executor.shutdown() followed by awaitTermination with a timeout.
- Forceful shutdown via shutdownNow() if tasks do not finish in time.
- Proper interrupt handling in the shutdown path.
WHY this matters:
- Shows a production-friendly pattern that combines high-level concurrency utilities:
- ExecutorService handles thread lifecycle and pooling for resource efficiency.
- CyclicBarrier ensures all subsystems reach the same readiness checkpoint before continuing.
- Demonstrates robust program termination and resource cleanup, which is essential in real systems.
- Practical for real-world cases such as orchestrating microservice initialization, multi-component startup, and phased pipelines.
HOW it works:
1. Create CyclicBarrier with party count equal to numberOfSubsystems and a barrier action that prints a completion message.
2. Create a fixed thread pool ExecutorService and submit one Subsystem task per component.
3. Each Subsystem sleeps to simulate startup work, then calls barrier.await(). When the last party arrives:
- Barrier action runs.
- All participant threads are released concurrently to proceed.
4. Main calls executor.shutdown() and waits up to 10 seconds for termination.
- If threads fail to terminate, main triggers executor.shutdownNow() to interrupt remaining tasks and logs forcing shutdown.
5. Subsystem code handles InterruptedException by restoring interrupt status and BrokenBarrierException by logging an error.
NOTES, GOTCHAS, and RECOMMENDATIONS:
- Use awaitTermination with a reasonable timeout to avoid indefinite blocking in main.
- Always handle BrokenBarrierException in tasks; it can occur if another thread is interrupted or the barrier is reset.
- If subsystem initialization is long-running or can hang, prefer shutdownNow() plus cooperative interruption in the task logic.
- For many short tasks or dynamic workloads, consider ForkJoinPool or work-stealing to improve throughput and load balancing.
- If barrier action performs heavy work, run that action in a separate thread or keep it lightweight to avoid delaying participants.
- For multi-phase synchronization, reuse CyclicBarrier across phases, or create one barrier per phase depending on complexity.
PERF and HARDWARE CONSIDERATIONS:
- Thread count should generally match available hardware threads for CPU bound tasks. For IO-bound initialization, a larger pool may be acceptable.
- On NUMA systems, consider data and thread affinity for improved cache locality when initialization touches large shared state.
SHORT KEY:
cyclicbarrier executorservice subsystem startup graceful-shutdown barrier-action.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9d4387e commit cbc7650
File tree
1 file changed
+66
-0
lines changed- Section19MultiThreading/ThreadPooling/ExecutorsFramework/ExecutorsServices/src
1 file changed
+66
-0
lines changedLines changed: 66 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
0 commit comments