Commit 0a03462
committed
feat: add ScheduledExecutorService demo with fixed-rate and delayed shutdown
WHAT was added:
- Example program using ScheduledExecutorService to demonstrate periodic and delayed task execution.
- Scheduled a repeating task with scheduleAtFixedRate() that prints a message every 5 seconds after initial delay.
- Scheduled a one-time shutdown task with schedule() that triggers graceful shutdown after 20 seconds.
- Code comments explaining differences between:
- schedule() → run once after a delay
- scheduleAtFixedRate() → run at a fixed rate regardless of task duration
- scheduleWithFixedDelay() → run with a fixed delay after previous task completion
WHY this matters:
- Shows practical usage of ScheduledExecutorService for tasks like cron jobs, background health checks, or periodic monitoring.
- Demonstrates scheduling patterns (fixed rate vs fixed delay) and highlights their semantic differences.
- Includes proper shutdown scheduling to stop executor service gracefully after demonstration period.
HOW it works:
1. Create ScheduledExecutorService with a single-thread pool.
2. Call scheduleAtFixedRate() with:
- initial delay = 5s
- period = 5s
- TimeUnit.SECONDS
This runs the task periodically at a fixed rate.
3. Call schedule() with a 20s delay to run a shutdown task that calls scheduler.shutdown().
4. After shutdown, no new tasks are scheduled, and the executor terminates once running tasks finish.
NOTES and GOTCHAS:
- Fixed-rate tasks try to maintain exact start times. If execution takes longer than period, tasks may "pile up".
- Fixed-delay tasks always wait the specified delay after completion, preventing pileup.
- Always shut down executors explicitly to release resources; in production, use awaitTermination() for clean shutdown.
- Be mindful of long-running tasks in scheduleAtFixedRate(), as they can cause thread starvation or memory pressure.
- Use multiple threads in pool if tasks are long-running or blocking.
PERF considerations:
- For CPU-bound periodic tasks, keep thread pool close to CPU core count.
- For IO-bound periodic tasks (like logging, polling, or health checks), a larger pool may be needed to overlap waits.
- Excessively frequent scheduling (e.g., sub-second intervals) may cause high scheduling overhead.
KEYWORDS:
scheduledexecutorservice fixedrate fixeddelay periodic-tasks graceful-shutdown.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent cbc7650 commit 0a03462
File tree
1 file changed
+33
-0
lines changed- Section19MultiThreading/ThreadPooling/ExecutorsFramework/ScheduledExecutorsServices/src
1 file changed
+33
-0
lines changedLines changed: 33 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 | + | |
0 commit comments