Member-only story

How to do Asynchronous Programming With Java

Using CompletableFuture to implement an asynchronous use case

Suraj Mishra
Javarevisited
2 min readNov 7, 2022

--

𝗖𝗼𝗻𝘀𝗶𝗱𝗲𝗿 𝘀𝘂𝗯𝘀𝗰𝗿𝗶𝗯𝗶𝗻𝗴 𝘁𝗼 𝘁𝗵𝗲 𝗻𝗲𝘄𝘀𝗹𝗲𝘁𝘁𝗲𝗿 𝘁𝗼 𝘀𝘁𝗮𝘆 𝗮𝗵𝗲𝗮𝗱 𝗶𝗻 𝗝𝗮𝘃𝗮 & 𝗦𝗽𝗿𝗶𝗻𝗴 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗺𝗲𝗻𝘁

Introduction

  • When we write code generally we end up writing synchronized code most of the time.
  • But in many situations, it makes sense not to block the execution and execute the code asynchronously.
  • In this article, we will consider one use case where writing asynchronous code makes sense.

Use Case

  • Consider a user apply for a product such as a credit card or debit card from front-end UI.
  • Now once at the backend, we receive user requests, we perform n number of use cases such as validation, transformation, persist to DB and sending acknowledgment email, etc.
  • Now all of these processes don't have to be synchronous. For example, we can send emails to customers irrespective of saving them to the database.
  • So we can execute this piece of logic asynchronously.

Product Type

  • Product type is enum that represents the types of…

--

--

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Suraj Mishra
Suraj Mishra

Written by Suraj Mishra

Staff Software Engineer @PayPal ( All opinions are my own and not of my employer )

Responses (4)