Completable Future
Vivek Gohil
What is it used for?
• Perform possible asynchronous(non-blocking) computation and
trigger dependent computations which could also be asynchronous.
Basics of Asynchronous Operation
main thread separate thread
Task
❌
Runnable
Basics of Asynchronous Operation
main thread separate thread
Task Callable
Runnable (vs) Callable
• Runnable (vs) Callable comes into point when we are using Executer
framework.
• ExecutorService is a subinterface of Executor, which accepts both
Runnable and Callable tasks.
• Earlier Multi-Threading can be achieved using Interface
Runnable(Since 1.0), but here the problem is after completing the
thread task we are unable to collect the Threads information. In-order
to collect the data we may use Static fields.
Working with Callable and Futures
• Lets Check small code example
Visualize The Callable And Future Example
thread-pool
Task 2
Task 1 Task 3 …
Blocking queue
t0 t1 t2 t3 …. t10
Fetch next task
From queue
Execute it
main thread
Future<Integer> future = service.submit(new Task());
placeholder
Once result is ready
3 Set value of placeholder
Visualize The Callable And Future Example
thread-pool
Task 2
Task 1 Task 3 …
Blocking queue
t0 t1 t2 t3 …. t10
Fetch next task
From queue
Execute it
main thread
Future<Integer> future = service.submit(new Task());
placeholder
Once result is ready
3 Set value of placeholder
future.get()
blcoked
runnable
Run dependent taks
There Is A Problem
thread-pool
Task 2
Task 1 Task 3 …
Blocking queue
t0 t1 t2 t3 …. t10
Fetch next task
From queue
Execute it
main thread
for 1..4
service.submit(new Task())
Once result is ready
future.get()
blcoked
runnable
Run dependent taks
7 5
1 7 5
Dependent tasks
Fetch Order
Enrich Order
payment
Dispatch
Send Email
For All orders
Independent Flows
Fetch Order
Enrich Order
payment
Dispatch
Send Email
Fetch Order
Enrich Order
payment
Dispatch
Send Email
Fetch Order
Enrich Order
payment
Dispatch
Send Email
Independent Flow
……………….

Completable Future java 8 Features with example

  • 1.
  • 2.
    What is itused for? • Perform possible asynchronous(non-blocking) computation and trigger dependent computations which could also be asynchronous.
  • 3.
    Basics of AsynchronousOperation main thread separate thread Task ❌ Runnable
  • 4.
    Basics of AsynchronousOperation main thread separate thread Task Callable
  • 5.
    Runnable (vs) Callable •Runnable (vs) Callable comes into point when we are using Executer framework. • ExecutorService is a subinterface of Executor, which accepts both Runnable and Callable tasks. • Earlier Multi-Threading can be achieved using Interface Runnable(Since 1.0), but here the problem is after completing the thread task we are unable to collect the Threads information. In-order to collect the data we may use Static fields.
  • 6.
    Working with Callableand Futures • Lets Check small code example
  • 7.
    Visualize The CallableAnd Future Example thread-pool Task 2 Task 1 Task 3 … Blocking queue t0 t1 t2 t3 …. t10 Fetch next task From queue Execute it main thread Future<Integer> future = service.submit(new Task()); placeholder Once result is ready 3 Set value of placeholder
  • 8.
    Visualize The CallableAnd Future Example thread-pool Task 2 Task 1 Task 3 … Blocking queue t0 t1 t2 t3 …. t10 Fetch next task From queue Execute it main thread Future<Integer> future = service.submit(new Task()); placeholder Once result is ready 3 Set value of placeholder future.get() blcoked runnable Run dependent taks
  • 9.
    There Is AProblem thread-pool Task 2 Task 1 Task 3 … Blocking queue t0 t1 t2 t3 …. t10 Fetch next task From queue Execute it main thread for 1..4 service.submit(new Task()) Once result is ready future.get() blcoked runnable Run dependent taks 7 5 1 7 5
  • 10.
    Dependent tasks Fetch Order EnrichOrder payment Dispatch Send Email For All orders
  • 11.
    Independent Flows Fetch Order EnrichOrder payment Dispatch Send Email Fetch Order Enrich Order payment Dispatch Send Email Fetch Order Enrich Order payment Dispatch Send Email Independent Flow ……………….