Skip to content

Commit 672e54f

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

Section19MultiThreading/ThreadPooling/src/ExecutorsFramework/FutureExample.java renamed to Section19MultiThreading/ThreadPooling/ExecutorsFramework/src/ExecutorsFramework/FutureExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
1717
executorService.shutdown();
1818

1919
}
20-
}
20+
}

0 commit comments

Comments
 (0)