From the course: Advanced Java: Threads and Concurrency

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Implementing CompletableFuture

Implementing CompletableFuture - Java Tutorial

From the course: Advanced Java: Threads and Concurrency

Implementing CompletableFuture

- [Lecturer] CompletableFuture API is written in such a way that it can utilize functional programming constructs. So expect to see some lambda expressions in this demo. In this first example here in line eight, I call the method supplyAsync to run a task asynchronously. It returns a CompletableFuture. It takes a supply object as a parameter. It's a type of object that can encapsulate the asynchronous task that returns the result. So in lines nine to 17, we implement the get method of supplier to access customer loyalty points data over the network and return the result. I'm just returning a hard coded value of 200 points. Now this code can be replaced with code that uses a lambda expression like this. Then let's say I want to add bonus points to this result and calculate total points. That will be a further action on the future's result. So in line 31, I attach a call back using thenApply. ThenApply takes in a function within which we can further process our results. That function is…

Contents