Skip to content

Commit d286412

Browse files
committed
docs(concurrency): add detailed notes and examples on Executors, ExecutorService, and ScheduledExecutorService
WHAT was added: - Comprehensive explanation of the Executor Framework in Java. - Clear breakdown of: • Executors utility class (factory methods like newFixedThreadPool, newCachedThreadPool, etc.) • ExecutorService interface (submit, invokeAll, invokeAny, shutdown, etc.) • ScheduledExecutorService interface (schedule, scheduleAtFixedRate, scheduleWithFixedDelay). - Comparison table showing differences in purpose, features, and usage. - Example programs: • ExecutorsExample → demonstrates fixed thread pool with 3 threads executing 5 tasks. • ExecutorServiceExample → shows `submit()` with Callable and retrieving result via Future. • ScheduledExecutorServiceExample → demonstrates scheduling tasks at fixed intervals. WHY this matters: - Simplifies understanding of Java’s concurrency abstractions. - Highlights advantages of using thread pools over manual thread creation. - Demonstrates task submission, scheduling, and lifecycle management in practice. - Provides a quick reference for when to choose Executors vs ExecutorService vs ScheduledExecutorService. HOW it works: - Executors: Utility class, only creates thread pools. - ExecutorService: Manages thread pools, executes tasks, supports results (Future). - ScheduledExecutorService: Extends ExecutorService, adds scheduling for delayed/periodic tasks. - Example outputs included to illustrate thread reuse and scheduling behavior. HINTS: - Always shut down ExecutorService to free resources (`shutdown()` or `shutdownNow()`). - Use fixed thread pools to limit concurrency and cached pools for dynamic scaling. - For periodic tasks, prefer `ScheduledExecutorService` instead of `Timer` (better thread safety). - Use `Future.get()` carefully — it blocks until result is ready. WHEN TO USE: - Executors → quick creation of thread pools. - ExecutorService → advanced task submission and result handling. - ScheduledExecutorService → scheduling periodic or delayed tasks. KEYWORDS: executors executorservice scheduledexecutorservice threadpool future concurrency java. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 5d381e2 commit d286412

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed
Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
Understanding Executors, ExecutorService, and ScheduledExecutorService in Java
1+
Understanding Executors, ExecutorService, and ScheduledExecutorService in Java:
2+
3+
The Executor Framework in Java provides a high-level API to manage threads efficiently.
24

3-
The Executors Framework in Java provides a high-level API to manage threads efficiently.
45
Instead of manually creating and managing threads, we use executors to handle task execution in a thread pool.
56

67
1️⃣ Executors
78
🔹 Executors is a utility class that provides factory methods to create various types of ExecutorService implementations.
89
🔹 It simplifies thread pool management.
910

10-
Common Methods in Executors
11+
Common Methods in Executors:
1112

1213
| Method | Description |
1314
|-----------------------------|------------------------------------------------------------------|
@@ -43,16 +44,14 @@ Task executed by: pool-1-thread-3
4344
Task executed by: pool-1-thread-1
4445
Task executed by: pool-1-thread-2
4546

46-
✅ Explanation:
47-
47+
Explanation:
4848
- The thread pool has 3 threads.
4949
- It reuses them to run 5 tasks.
5050

5151
2️⃣ ExecutorService (Interface)
5252
🔹 ExecutorService extends Executor and adds methods to manage thread lifecycle & task execution.
5353

54-
55-
Key Methods in `ExecutorService`]
54+
Key Methods in ExecutorService:
5655

5756
| Method | Description |
5857
|------------------------------------------- |-----------------------------------------------------------------|
@@ -85,29 +84,26 @@ public class ExecutorServiceExample {
8584
executor.shutdown();
8685
}
8786
}
88-
```
8987

9088
Output:
91-
9289
Task submitted...
9390
Computing...
9491
Result: 42
9592

96-
✅ Explanation:
97-
93+
Explanation:
9894
- submit() is used instead of `execute()` because it returns a `Future<Integer>` to get the result.
9995

10096
3️⃣ ScheduledExecutorService (Interface) and ScheduledExecutorService extends ExecutorService and provides
10197
methods to schedule tasks with delays or fixed intervals.
10298

103-
Key Methods in ScheduledExecutorService
99+
Key Methods in ScheduledExecutorService:
104100

105-
| Method | Description |
101+
| Method | Description |
106102
|-------------------------------------------------------------------|-------------------------------------------------|
107103
| schedule(Runnable, delay, TimeUnit) | Runs task once after a delay. |
108104
| scheduleAtFixedRate(Runnable, initialDelay, period, TimeUnit) | Runs task repeatedly at a fixed rate. |
109-
| scheduleWithFixedDelay(Runnable, initialDelay, delay, TimeUnit) | Runs task repeatedly with a |
110-
fixed delay between completions. |
105+
| scheduleWithFixedDelay(Runnable, initialDelay, delay, TimeUnit) | Runs task repeatedly with a -- |
106+
| | -- fixed delay between completions. |
111107

112108
Example: Using scheduleAtFixedRate();
113109

@@ -133,11 +129,11 @@ Running at: 1700000003000
133129
Running at: 1700000006000
134130
Running at: 1700000009000
135131

136-
Explanation:
132+
Explanation:
137133

138134
The task starts after 1 second and then runs every 3 seconds.
139135

140-
Comparing Executors, ExecutorService, and ScheduledExecutorService**
136+
Comparing Executors, ExecutorService, and ScheduledExecutorService:
141137

142138
| Feature | Executors | ExecutorService | ScheduledExecutorService |
143139
|-------------------------|----------------------|------------------------|--------------------------|
@@ -148,8 +144,7 @@ Comparing Executors, ExecutorService, and ScheduledExecutorService**
148144
| Handles Futures? | ❌ No | ✅ Yes | ✅ Yes |
149145

150146

151-
🔥 When to Use What?
152-
147+
When to Use What?
153148
✔ Use Executors when you need a simple way to create a thread pool.
154149
✔ Use ExecutorService when you need advanced task management (like Future).
155-
✔ Use ScheduledExecutorService when you need periodic task execution.
150+
✔ Use ScheduledExecutorService when you need periodic task execution.

0 commit comments

Comments
 (0)