Skip to content

Soumya1910/Java_Async_CompletableFuture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Asynchronous Programming in Java

CompletableFuture Interafce

CompletetableFuture is a building block and framework with different methods for composing, combining, executing in async process and handling errors.

public class CompletableFuture<T> implements Future<T>, CompletionStage<T>
  • Future interface was implemented in Java 5 with very limited functions. It doesn't have any methods to combine these execution and handling possible errors.
  • CompleteStage is implemented in Java 8 with lot of advanced functions.

These are the main disadvantages of Future Interface.

  • It cannot be manually completed.
  • Multiple Futures cannot be chained together and proceed further.
  • No proper exception handling mechanism.

runAsync():

If we want to run some background task but doesn't want to return anything, then we should use runAsync() method. It takes runnable Object and returns CompletableFuture.

CompletableFuture.runAsync(Runnable);
CompletableFuture.runAsync(Runnable, Executor);

supplyAsync()

if we want to run some task in background but need to return some value to main thread then we should use supplyAsync(). It takes Supplier and returns CompletableFuture.

CompletableFuture.supplyAsync(Supplier<T>);
CompletableFuture.supplyAsync(Supplier<T>, Executor);

If we are not passing any custom executor, then thread will be taken from forkJoin global Thread pool.

About

It describes different methods of CompletableFuture with practical example.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages