Skip to content

Commit c028fce

Browse files
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

1 file changed

+0
-4
lines changed

Section19MultiThreading/ThreadPooling/src/ExecutorsFramework/CallableMethod.java renamed to Section19MultiThreading/ThreadPooling/RunnableVsCallable/src/CallableMethod.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package ExecutorsFramework;
2-
31
import java.util.Arrays;
42
import java.util.List;
53
import java.util.concurrent.*;
@@ -31,9 +29,7 @@ public static void main(String[] args) throws Exception {
3129
for (Future<Integer> f : futures) {
3230
System.out.println(f.get());
3331
}
34-
3532
executorService.shutdown();
3633
System.out.println("Hello World");
3734
}
3835
}
39-
//InvokeALL() method done.

0 commit comments

Comments
 (0)