Skip to content

Commit b49bec5

Browse files
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 changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.concurrent.ExecutorService;
2+
import java.util.concurrent.Executors;
3+
4+
public class Main2 {
5+
public static void main(String[] args) {
6+
/*
7+
* newFixedThreadPool(int nThreads):
8+
* ---------------------------------
9+
* - Creates a thread pool with a fixed number of threads.
10+
* - If all threads are busy, new tasks wait in a queue until a thread becomes free.
11+
* - Suitable when the number of threads should be constant,
12+
* e.g., controlling parallelism or limiting resource usage.
13+
14+
* Ek fixed size ka thread pool banata hai.
15+
* Agar saare threads busy ho jayein, to naye tasks queue mein wait karte hain
16+
* jab tak koi thread free na ho jaye.
17+
* Isko tab use karo jab threads ki count constant rakhni ho,
18+
* jaise limited parallelism ya resource control ke liye.
19+
*/
20+
Executors.newFixedThreadPool(2);
21+
22+
/*
23+
* newCachedThreadPool():
24+
* ----------------------
25+
* - Creates a thread pool that dynamically adjusts the number of threads
26+
* based on demand.
27+
* - New threads are created as needed, and idle threads are reused if available.
28+
* - Suitable for applications with a large number of short-lived tasks
29+
* or workloads that vary significantly.
30+
*
31+
* Ek aisa thread pool banata hai jo dynamically threads ko adjust karta hai.
32+
* Jitne tasks aate hain, utne threads create kar deta hai (agar free threads available
33+
* ho to unhe reuse karta hai).
34+
* Ye short-lived tasks ke liye best hai, ya jab workload variable ho
35+
* (kabhi zyada load, kabhi kam load).
36+
*/
37+
ExecutorService executorService = Executors.newCachedThreadPool();
38+
}
39+
}

0 commit comments

Comments
 (0)