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

#2 Java Programming CompletableFuture tutorial

Available In Following Subtitles
English
Variant 1
Posted on:
Java's Completablefuture 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 the Java 8 programming Completablefuture 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
In the last lesson, I showed you how to use
0:04
CompletedView features, run async and supply async
0:09
Before we get on with this very lesson
0:13
Let us demonstrate the thread that these processes actually run
0:19
What do I mean by that?
0:21
Now, in a process, it's actually possible
0:27
check out which trade that process is running on. I can actually print out
0:34
trade.currentTrade() this will print out
0:40
tells the current thread this long process is going to run on. If I go on to the main method
0:46
and I write long network process, let's run this. So if you look at it, it's running in the main thread.
1:00
That is why it's blocking the main thread when it takes some time to execute.
1:05
Now this time let us use completable features. So if I say completable feature dot supply async.
1:14
I come over here and I run long network process. I can put in six. And now see then accept
1:29
once the file is ready. Let's print it out. Alright. So to make this really nice, let
1:43
us slip the main thread a little bit. When we run this, can you see that? Now the thread is in the fork
2:02
join pool, no longer in the main thread. That's just to prove that this is running on a different
2:10
thread. I want to treat then apply. Normally if I have a contributory feature and I say supply a sync
2:23
I could have a long network process and I put in
2:30
7. As it didn't accept, once it's ready, you can print it out. That's fine. Let's say I have a
2:45
simple method that takes in a value. Now if that value is even, it will add 1 to the value. If it's
2:56
not even to add three to the value and return back the value. So where am I going with this?
3:05
Currently if I come here, I'm sleepy little. Let us remove this thread statement,
3:12
since we already know the thread it's running on. When I run this, it's going to print out 70. Yes,
3:21
because the collect7 host long network process
3:31
multiplies it by 10 and returns it
3:34
So, what if after a long network process
3:40
probably I want to take the value
3:43
work on it a little bit, probably do some processes
3:47
and get a refined value
3:49
probably, let's say this network process gets me an ID
3:53
then I have another method to get me the username of the ID and I have more operations before I
4:01
finally get the value. What I can do here is then apply. So I take in the integer and send it to
4:14
perform some network operations, perform some operations and I put in the integer.
4:22
So this will come over here, return the value, come to then apply and then before accepting.
4:31
So what is going to happen is 7 is going to be sent to long network process and it will end up
4:40
being 7 times 10 which is 70. Then 70 will come to then apply and send 70 to the sum operations
4:50
method and because 70 is even it will add 1 to 70 and then in the then accepts we're now going to
5:01
print out what we accepted from the then apply look at it as a pipeline so it comes up goes down
5:10
and similar to the structure of streams so going from one method to the next each working on the
5:18
values. So there we have it 71 is the answer and if you wish you can as well come and have another
5:31
then apply. This time we'll perform some operations again. So if you look at this you can have 71
5:41
at this moment 71 is not an even number. So to actually add 3 to 71, which will make it 74.
5:52
This will result in 70. This will process on 70 because it's even, will add 1 to it and return
6:00
the value. Once this gets 71, it sees that 71 is odd. So I'll add 3 to it, according to my code.
6:10
Thank you.
6:11
and then in then accept finally we can print it out
6:15
let's run this we should expect 74
6:19
yeah we got it
6:21
so you can use then apply and then spice up your code
6:26
this is very useful and very lovely as you can see
shape-icon

Download extension to view full transcript.

chrome-icon Install Tubelator On Chrome