KEMBAR78
Muti Threading Future & CompletableFuture | PDF
0% found this document useful (0 votes)
19 views9 pages

Muti Threading Future & CompletableFuture

The document compares Future and CompletableFuture in Java, highlighting that Future, introduced in Java 5, represents an asynchronous computation result but is blocking, while CompletableFuture, introduced in Java 8, offers non-blocking, asynchronous programming with capabilities for chaining and combining tasks. Key methods of Future include get(), cancel(), isDone(), and isCancelled(), whereas CompletableFuture provides built-in exception handling and supports complex asynchronous logic. The document concludes by advising the use of Future for simple tasks and CompletableFuture for more complex scenarios.

Uploaded by

Mbarki Chady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views9 pages

Muti Threading Future & CompletableFuture

The document compares Future and CompletableFuture in Java, highlighting that Future, introduced in Java 5, represents an asynchronous computation result but is blocking, while CompletableFuture, introduced in Java 8, offers non-blocking, asynchronous programming with capabilities for chaining and combining tasks. Key methods of Future include get(), cancel(), isDone(), and isCancelled(), whereas CompletableFuture provides built-in exception handling and supports complex asynchronous logic. The document concludes by advising the use of Future for simple tasks and CompletableFuture for more complex scenarios.

Uploaded by

Mbarki Chady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Future vs CompletableFuture in

Java
Asynchronous programming made easier with Java!
What is Future?
- Introduced in Java 5
- Represents async computation result
- Result available after task completes

Future<String> future = executor.submit(() -> "Hello");


String result = future.get(); // Blocks
Key Methods in Future
Method | Description
------------- | -----------------------------
get() | Waits for and returns result
cancel() | Attempts to cancel execution
isDone() | Checks if task is completed
isCancelled() | Checks if task was cancelled
Enter CompletableFuture (Java 8+)

- More powerful than Future


- Supports non-blocking, async programming
- Allows chaining and combining tasks
CompletableFuture Basic Example
CompletableFuture.supplyAsync(() -> "Hello")
.thenApply(s -> s + " World")
.thenAccept(System.out::println);

// Output: Hello World


Comparison
Feature | Future | CompletableFuture
-------------------|------------------|----------------------
Blocking | Yes | No (async)
Chaining | No | Yes
Combining Futures | No | Yes
Exception Handling | Manual try/catch | Built-in (exceptionally)
Exception Handling Example
CompletableFuture.supplyAsync(() -> 10 / 0)
.exceptionally(ex -> {
System.out.println("Error: " + ex);
return 0;
})
.thenAccept(System.out::println);
Where to Use Them?
Use Future for:
- Simple async result
- Blocking acceptable

Use CompletableFuture for:


- Complex async logic
- Non-blocking flow
- Chaining multiple tasks
Final Thought
Choose the right tool:
Future – basic async handling
CompletableFuture – modern async toolkit

#Java #CompletableFuture #Multithreading #AsynchronousProgramming #BackendTips

You might also like