#1 Java Programming CompletableFuture tutorial
Java's Completable Future is an extension to Java's Future API which was introduced in Java 5.
In this tutorial series, you are going to learn how to use Java programming Completable futures for asynchronous programming. You can use completable futures to run processes in the background using a different thread, thereby avoiding blocking the main thread.
You would learn how to use supply async, run async, then accept, then apply, exceptionally and much more.
#LemubitAcademy
Video Summary & Chapters
No chapters for this video generated yet.
Video Transcript
when Java 8 was released. Everyone was interested in the new Java streams, but Java 8 brought
modern streams. We also have completable features. A lot of people didn't really look at completable
features, but it's actually a wonderful tool. You can use completable features to run code
asynchronously. So let's look at our typical example. So we have a program here, which I
want to use to simulate a network process. It simply collects a value
returns a multiple of that value. So in this case, I'm going to multiply the value times 10. Before
returning the value, I'm going to sleep to trade for two seconds. Now, this is just to simulate how
a network call would behave. Sometimes it could be longer actually. So let's come over to our main
method so this should return 50 let's run this you'll notice that it actually
took a long while before printing out 50 to prove that just do this I put this so
that you can know when the code actually starts running and how long it takes it
to stop. Let's make this three seconds. Run this. So the code is started but then took a while
before ending. Now this can cause a delay in your application. So if I call this long network process
this whole thread is occupied. Therefore your application will appear to actually be hanging.
So you actually want to run this in the background. Now with completable features,
you can do just that. And you can even do more. Let me explain. In order to use
completable features, I'll simply say, completable feature dot supply async. Now
this takes in a supplier so I am going to use a lambda expression I would put a
link to a very good tutorial on lambda expression you can check the tutorial on
Java brains. It's actually a good series or lambda expressions. If I expand this, this
is what you actually get. So this is the supplier. So it's returning the value of the long network
process. If we run this, you can see the code executes quite quickly and nothing is
returned. Let's come over here and say, process called. So let's run this program. So if you
look at the output, it called starting process and immediately called process
called. So what happens is, it prints out this and calls this. This ends up running in a different
thread. Then it calls this because this is running in a different thread. It doesn't stop this from
running. So by using this, so by using this, you have less interruptions in your application.
Now, how do we get the value returned from long network process?
Because long network process returns the value times 10.
I will come here and add one other function called then accept.
Once you run your process here, and it returns a value, then accept is saying, after this
process is done give me the value now you can work with the value now because
learn
network process is returning an integer, then accept will supply you with an integer value.
Now, this is just a variable name, it's an integer, but then you're familiar with lambda expressions.
IntelliJ just named it integer. Now you can name this variable anything but for now,
I'm just calling it integer. It has the type integer. Even though it's an integer,
integer, the name must not be integer, I can as well just come and say, value, I'll have
to come here and change it to value as well. So if I run this, nothing comes out. Why?
Because this is a totally different trade. What we can do is to slow down this main thread
so that this can complete. So in a normal application, you wouldn't have this issue,
because your application is running continuously. In our simple Java program, once the code is
finished running, the whole application finishes. What we can do is we can say, sleeping
for a little bit. So I'll come over here and say thread dot sleep. Let's sleep for three
seconds as well. It was what happened to create a method for this topic. Sleep a little and
I'll put this inside here.
for me I'll write sleepy little
I should be seeing that code every time I remove this I'll write sleepy little
and under it I'll write done sleeping
so we start the process call the completable feature then the completable feature prints