Commit 2b8d56e
committed
feat(executors): implement factorial computation using ExecutorService with fixed thread pool
WHAT was added:
- Java demo program (`Main`) showcasing `ExecutorService` with `Executors.newFixedThreadPool(3)`.
- Computes factorials of numbers 1–9 concurrently across 3 worker threads.
- Introduced `executor.submit()` for task submission instead of manual thread creation.
- Used `executor.shutdown()` and `awaitTermination()` to gracefully stop the pool.
- Added simulated workload via `Thread.sleep(1000)` to demonstrate concurrent execution.
- Printed total execution time for performance comparison.
WHY this matters:
- Demonstrates how to offload CPU-bound or time-consuming tasks (factorial calculation) to a managed thread pool.
- Highlights benefits of thread pooling:
• Efficient reuse of worker threads.
• Controlled concurrency (3 threads max at a time).
• Automatic queuing of extra tasks until threads free up.
- Shows best practices with graceful shutdown (`shutdown()` + `awaitTermination()`).
HINTS / NEXT STEPS:
- Try replacing `newFixedThreadPool(3)` with:
• `newSingleThreadExecutor()` → sequential execution.
• `newCachedThreadPool()` → dynamic thread allocation.
- Use `Future<Long>` with `submit(Callable<Long>)` to capture and return factorial results.
- Experiment with different pool sizes to measure execution time differences.
KEYWORDS:
executors executorservice threadpool factorial concurrency fixedthreadpool java-multithreading graceful-shutdown.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent c412915 commit 2b8d56e
File tree
1 file changed
+52
-0
lines changed- Section19MultiThreading/ThreadPooling/ExecutorsFramework/src
1 file changed
+52
-0
lines changedLines changed: 52 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 | + | |
0 commit comments