Commit 672e54f
committed
feat: add FutureExample demonstrating Future with ExecutorService
WHAT was added:
- A new class `FutureExample` inside `ExecutorsFramework` package.
- Uses `ExecutorService` with `newSingleThreadExecutor()`.
- Submits a lambda task returning `42`.
- Demonstrates:
- `future.get()` to retrieve result.
- `future.isDone()` to check task completion.
- Proper shutdown of ExecutorService.
WHY this matters:
- Introduces `Future` API usage in Java concurrency.
- Shows how to retrieve results of asynchronous tasks.
- Highlights non-blocking task submission + blocking result retrieval.
- Serves as a minimal example for teaching/explaining Future basics.
HOW it works:
1. `executorService.submit(() -> 42)` submits a `Callable<Integer>` task.
2. A `Future<Integer>` is returned immediately (non-blocking).
3. `future.get()` blocks until the task completes and retrieves `42`.
4. `future.isDone()` confirms task completion.
5. `executorService.shutdown()` ensures graceful resource cleanup.
USE CASES:
- Running background tasks where results are required later.
- Asynchronous computation in applications like:
- API requests
- Database queries
- File processing
GOTCHAS:
- `future.get()` is blocking; it waits until task completion.
- If the task throws an exception, `ExecutionException` is wrapped and thrown.
- Must call `shutdown()` to release executor resources.
KEYWORDS:
future executorservice singlethreadexecutor async callable blocking get isDone.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent b49bec5 commit 672e54f
File tree
1 file changed
+1
-1
lines changed- Section19MultiThreading/ThreadPooling/ExecutorsFramework/src/ExecutorsFramework
1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
0 commit comments