Commit c028fce
committed
feat(callable): demonstrate Callable with ExecutorService using invokeAll()
WHAT was added:
- Implemented `CallableMethod` class that:
• Creates 3 Callable tasks (`callable1`, `callable2`, `callable3`) returning integers.
• Uses `ExecutorService` with a fixed thread pool of size 3.
• Submits all tasks at once via `invokeAll()`, ensuring all complete or timeout within 1000ms.
• Collects `Future<Integer>` objects and retrieves results using `get()`.
- Gracefully shuts down the executor and prints confirmation ("Hello World").
WHY this matters:
- Demonstrates difference between `Runnable` and `Callable`:
• `Runnable` → no return value.
• `Callable<T>` → returns a result (via `Future<T>`).
- Shows how `invokeAll()` simplifies batch task submission and synchronization.
- Adds timeout support (`1000ms`) to prevent indefinite blocking if tasks take too long.
HINTS / NEXT STEPS:
- Experiment with `invokeAny()` to return the result of the first successfully completed task.
- Replace `System.out.println(f.get())` with proper aggregation or result processing.
- Explore behavior when one task exceeds the timeout (task gets cancelled).
- Compare with individual `submit(callable)` + `Future.get()` calls.
KEYWORDS:
callable invokeall future executorservice multithreading threadpool concurrency java.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 2b8d56e commit c028fce
File tree
1 file changed
+0
-4
lines changed- Section19MultiThreading/ThreadPooling/RunnableVsCallable/src
1 file changed
+0
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | 3 | | |
| |||
31 | 29 | | |
32 | 30 | | |
33 | 31 | | |
34 | | - | |
35 | 32 | | |
36 | 33 | | |
37 | 34 | | |
38 | 35 | | |
39 | | - | |
0 commit comments