Commit b49bec5
committed
docs: add explanation of newFixedThreadPool vs newCachedThreadPool
WHAT was added:
- Java class demonstrating creation of thread pools using Executors factory methods.
- Detailed inline documentation (in English + Hindi) for:
- Executors.newFixedThreadPool(int nThreads)
→ Creates a pool with fixed number of threads, queues extra tasks.
→ Best for controlled parallelism or resource-limited scenarios.
- Executors.newCachedThreadPool()
→ Creates a pool that dynamically grows/shrinks based on demand.
→ Best for many short-lived or bursty tasks.
WHY this matters:
- Thread pool choice impacts application performance, scalability, and resource usage.
- Developers often misuse thread pools; documentation clarifies when to use fixed vs cached.
- Helps understand how Executors factory methods map to real-world concurrency needs.
HOW it works:
1. Executors.newFixedThreadPool(2):
- Creates a pool of 2 threads.
- If >2 tasks are submitted, extras wait in an unbounded queue.
- Prevents uncontrolled thread creation but may increase latency under load.
2. Executors.newCachedThreadPool():
- Starts with 0 threads, creates new threads as tasks arrive.
- Reuses idle threads if available; threads idle for 60s are terminated.
- May create unlimited threads, which can overwhelm system if workload spikes.
USE CASES:
- FixedThreadPool:
- Database connections, CPU-bound processing, or predictable parallelism.
- CachedThreadPool:
- Network request handling, RPC, microservice endpoints, or highly variable loads.
GOTCHAS:
- CachedThreadPool can exhaust system resources if tasks flood in faster than they complete.
- FixedThreadPool can cause long queues and increased latency under heavy load.
- Always shut down thread pools explicitly to avoid memory leaks.
KEYWORDS:
executors fixedthreadpool cachedthreadpool threadpool concurrency.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 0a03462 commit b49bec5
File tree
1 file changed
+39
-0
lines changed- Section19MultiThreading/ThreadPooling/ExecutorsFramework/ScheduledExecutorsServices/src
1 file changed
+39
-0
lines changedSection19MultiThreading/ThreadPooling/ExecutorsFramework/ScheduledExecutorsServices/src/Main2.java
Lines changed: 39 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 | + | |
0 commit comments