SlideShare a Scribd company logo
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
……………….

More Related Content

ODP
Concurrent Programming in Java
PPTX
.NET Multithreading/Multitasking
PPTX
C#: Understanding ConfigureAwait(false)
PPTX
Parallel Programming
PPTX
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
PPTX
Neural Networks with Google TensorFlow
PPTX
Salesforce asynchronous apex
PDF
Java Multithreading Using Executors Framework
Concurrent Programming in Java
.NET Multithreading/Multitasking
C#: Understanding ConfigureAwait(false)
Parallel Programming
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Neural Networks with Google TensorFlow
Salesforce asynchronous apex
Java Multithreading Using Executors Framework

Similar to Completable Future java 8 Features with example (20)

PPTX
PDF
Here comes the Loom - Ya!vaConf.pdf
PPTX
Events for JavaScript event loop track.pptx
ODP
Java Concurrency, Memory Model, and Trends
PDF
Java concurrency
PPTX
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
PPTX
Java concurrency - Thread pools
PPTX
Threads
PPTX
Synchronization problem with threads
PDF
Concurrency and Thread-Safe Data Processing in Background Tasks
PPTX
Training – Going Async
PPTX
Inter thread communication &amp; runnable interface
PDF
Multiprocessing.pdf..............,.......
PDF
Multiprocessing.............,...........
PPTX
Node js for backend server development.
PPTX
Python UNIT-IV Multi Threading B.Tech CSE
PDF
Intake 38 12
PPTX
Parallel Programming Hands-On Presentation
PPTX
Thread syncronization
PPT
The Pillars Of Concurrency
Here comes the Loom - Ya!vaConf.pdf
Events for JavaScript event loop track.pptx
Java Concurrency, Memory Model, and Trends
Java concurrency
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Java concurrency - Thread pools
Threads
Synchronization problem with threads
Concurrency and Thread-Safe Data Processing in Background Tasks
Training – Going Async
Inter thread communication &amp; runnable interface
Multiprocessing.pdf..............,.......
Multiprocessing.............,...........
Node js for backend server development.
Python UNIT-IV Multi Threading B.Tech CSE
Intake 38 12
Parallel Programming Hands-On Presentation
Thread syncronization
The Pillars Of Concurrency
Ad

Recently uploaded (20)

PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharma ospi slides which help in ospi learning
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Lesson notes of climatology university.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Institutional Correction lecture only . . .
PDF
Insiders guide to clinical Medicine.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Computing-Curriculum for Schools in Ghana
Microbial diseases, their pathogenesis and prophylaxis
Pharma ospi slides which help in ospi learning
Microbial disease of the cardiovascular and lymphatic systems
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Module 4: Burden of Disease Tutorial Slides S2 2025
Anesthesia in Laparoscopic Surgery in India
Lesson notes of climatology university.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Supply Chain Operations Speaking Notes -ICLT Program
PPH.pptx obstetrics and gynecology in nursing
102 student loan defaulters named and shamed – Is someone you know on the list?
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Institutional Correction lecture only . . .
Insiders guide to clinical Medicine.pdf
Ad

Completable Future java 8 Features with example

  • 2. What is it used for? • Perform possible asynchronous(non-blocking) computation and trigger dependent computations which could also be asynchronous.
  • 3. Basics of Asynchronous Operation main thread separate thread Task ❌ Runnable
  • 4. Basics of Asynchronous Operation 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 Callable and Futures • Lets Check small code example
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. Dependent tasks Fetch Order Enrich Order payment Dispatch Send Email For All orders
  • 11. 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 ……………….