1. Tubelator AI
  2. >
  3. Videos
  4. >
  5. Science & Technology
  6. >
  7. #1 Java Programming CompletableFuture tutorial

#1 Java Programming CompletableFuture tutorial

Available In Following Subtitles
English
Variant 1
Posted on:
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
tubelator logo

Instantly generate YouTube summary, transcript and subtitles!

chrome-icon Install Tubelator On Chrome

Video Summary & Chapters

No chapters for this video generated yet.

Video Transcript

0:00
when Java 8 was released. Everyone was interested in the new Java streams, but Java 8 brought
0:11
modern streams. We also have completable features. A lot of people didn't really look at completable
0:19
features, but it's actually a wonderful tool. You can use completable features to run code
0:27
asynchronously. So let's look at our typical example. So we have a program here, which I
0:35
want to use to simulate a network process. It simply collects a value
0:40
returns a multiple of that value. So in this case, I'm going to multiply the value times 10. Before
0:47
returning the value, I'm going to sleep to trade for two seconds. Now, this is just to simulate how
0:56
a network call would behave. Sometimes it could be longer actually. So let's come over to our main
1:04
method so this should return 50 let's run this you'll notice that it actually
1:18
took a long while before printing out 50 to prove that just do this I put this so
1:29
that you can know when the code actually starts running and how long it takes it
1:33
to stop. Let's make this three seconds. Run this. So the code is started but then took a while
1:46
before ending. Now this can cause a delay in your application. So if I call this long network process
1:57
this whole thread is occupied. Therefore your application will appear to actually be hanging.
2:05
So you actually want to run this in the background. Now with completable features,
2:11
you can do just that. And you can even do more. Let me explain. In order to use
2:19
completable features, I'll simply say, completable feature dot supply async. Now
2:30
this takes in a supplier so I am going to use a lambda expression I would put a
2:38
link to a very good tutorial on lambda expression you can check the tutorial on
2:44
Java brains. It's actually a good series or lambda expressions. If I expand this, this
2:55
is what you actually get. So this is the supplier. So it's returning the value of the long network
3:04
process. If we run this, you can see the code executes quite quickly and nothing is
3:20
returned. Let's come over here and say, process called. So let's run this program. So if you
3:37
look at the output, it called starting process and immediately called process
3:43
called. So what happens is, it prints out this and calls this. This ends up running in a different
3:53
thread. Then it calls this because this is running in a different thread. It doesn't stop this from
4:02
running. So by using this, so by using this, you have less interruptions in your application.
4:12
Now, how do we get the value returned from long network process?
4:20
Because long network process returns the value times 10.
4:26
I will come here and add one other function called then accept.
4:34
Once you run your process here, and it returns a value, then accept is saying, after this
4:41
process is done give me the value now you can work with the value now because
4:47
learn
4:48
network process is returning an integer, then accept will supply you with an integer value.
4:55
Now, this is just a variable name, it's an integer, but then you're familiar with lambda expressions.
5:02
IntelliJ just named it integer. Now you can name this variable anything but for now,
5:09
I'm just calling it integer. It has the type integer. Even though it's an integer,
5:16
integer, the name must not be integer, I can as well just come and say, value, I'll have
5:21
to come here and change it to value as well. So if I run this, nothing comes out. Why?
5:31
Because this is a totally different trade. What we can do is to slow down this main thread
5:39
so that this can complete. So in a normal application, you wouldn't have this issue,
5:50
because your application is running continuously. In our simple Java program, once the code is
5:57
finished running, the whole application finishes. What we can do is we can say, sleeping
6:10
for a little bit. So I'll come over here and say thread dot sleep. Let's sleep for three
6:24
seconds as well. It was what happened to create a method for this topic. Sleep a little and
6:45
I'll put this inside here.
6:52
for me I'll write sleepy little
6:56
I should be seeing that code every time I remove this I'll write sleepy little
7:05
and under it I'll write done sleeping
7:13
so we start the process call the completable feature then the completable feature prints
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome