Commit c412915
committed
docs(executors): add deep dive on Executors.newCachedThreadPool()
WHAT was added:
- Comprehensive explanation of `Executors.newCachedThreadPool()`:
• Dynamically resizable thread pool that creates new threads as needed.
• Reuses idle threads (with 60s timeout) to reduce overhead.
• No fixed size; can grow indefinitely based on workload.
• Tasks are executed immediately (no queue by default).
- Two runnable Java examples:
1. Basic demo showing thread creation for multiple tasks.
2. Reuse demo showing how idle threads handle new tasks when spaced with delays.
- Comparison table:
• Contrasted `newCachedThreadPool()` with `newFixedThreadPool(n)`.
• Highlighted thread count, task handling, and idle timeout differences.
- Practical usage guidelines:
✔ Best for bursty workloads or unpredictable short-lived tasks.
✔ Avoid for long-running tasks (risk of unbounded thread growth).
- Pitfalls section:
• Risk of unbounded thread creation → may exhaust system resources.
• Thread starvation if tasks run too long.
• Recommendations to prefer `newFixedThreadPool()` or `ThreadPoolExecutor` with limits for controlled scenarios.
- Custom alternative:
• Provided example using `ThreadPoolExecutor` with `SynchronousQueue`.
• Demonstrated limiting max pool size while retaining cached behavior.
WHY this matters:
- `newCachedThreadPool()` is a commonly misunderstood executor type.
- Developers often misuse it for long-running tasks, leading to performance issues.
- This doc + examples clarify correct use cases, trade-offs, and safer alternatives.
HINTS / NEXT STEPS:
- Always call `shutdown()` or `awaitTermination()` after task submission.
- Use `newCachedThreadPool()` for short tasks in highly concurrent, bursty environments (e.g., request handling).
- For fine-grained control → configure `ThreadPoolExecutor` manually.
KEYWORDS:
executors threadpool newCachedThreadPool concurrency java concurrency utilities synchronousqueue threadreuse.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e988673 commit c412915
File tree
1 file changed
+8
-7
lines changed- Section19MultiThreading/ThreadPooling/ExecutorsFramework/src/TXTFiles
1 file changed
+8
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | | - | |
4 | | - | |
| 3 | + | |
5 | 4 | | |
6 | | - | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
136 | 137 | | |
137 | 138 | | |
138 | 139 | | |
139 | | - | |
| 140 | + | |
140 | 141 | | |
141 | 142 | | |
142 | 143 | | |
143 | | - | |
| 144 | + | |
144 | 145 | | |
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
148 | | - | |
| 149 | + | |
0 commit comments