#2 Java Programming CompletableFuture tutorial
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
Video Summary & Chapters
No chapters for this video generated yet.
Video Transcript
In the last lesson, I showed you how to use
CompletedView features, run async and supply async
Before we get on with this very lesson
Let us demonstrate the thread that these processes actually run
What do I mean by that?
Now, in a process, it's actually possible
check out which trade that process is running on. I can actually print out
trade.currentTrade() this will print out
tells the current thread this long process is going to run on. If I go on to the main method
and I write long network process, let's run this. So if you look at it, it's running in the main thread.
That is why it's blocking the main thread when it takes some time to execute.
Now this time let us use completable features. So if I say completable feature dot supply async.
I come over here and I run long network process. I can put in six. And now see then accept
once the file is ready. Let's print it out. Alright. So to make this really nice, let
us slip the main thread a little bit. When we run this, can you see that? Now the thread is in the fork
join pool, no longer in the main thread. That's just to prove that this is running on a different
thread. I want to treat then apply. Normally if I have a contributory feature and I say supply a sync
I could have a long network process and I put in
7. As it didn't accept, once it's ready, you can print it out. That's fine. Let's say I have a
simple method that takes in a value. Now if that value is even, it will add 1 to the value. If it's
not even to add three to the value and return back the value. So where am I going with this?
Currently if I come here, I'm sleepy little. Let us remove this thread statement,
since we already know the thread it's running on. When I run this, it's going to print out 70. Yes,
because the collect7 host long network process
multiplies it by 10 and returns it
So, what if after a long network process
probably I want to take the value
work on it a little bit, probably do some processes
and get a refined value
probably, let's say this network process gets me an ID
then I have another method to get me the username of the ID and I have more operations before I
finally get the value. What I can do here is then apply. So I take in the integer and send it to
perform some network operations, perform some operations and I put in the integer.
So this will come over here, return the value, come to then apply and then before accepting.
So what is going to happen is 7 is going to be sent to long network process and it will end up
being 7 times 10 which is 70. Then 70 will come to then apply and send 70 to the sum operations
method and because 70 is even it will add 1 to 70 and then in the then accepts we're now going to
print out what we accepted from the then apply look at it as a pipeline so it comes up goes down
and similar to the structure of streams so going from one method to the next each working on the
values. So there we have it 71 is the answer and if you wish you can as well come and have another
then apply. This time we'll perform some operations again. So if you look at this you can have 71
at this moment 71 is not an even number. So to actually add 3 to 71, which will make it 74.
This will result in 70. This will process on 70 because it's even, will add 1 to it and return
the value. Once this gets 71, it sees that 71 is odd. So I'll add 3 to it, according to my code.
Thank you.
and then in then accept finally we can print it out
let's run this we should expect 74
yeah we got it
so you can use then apply and then spice up your code
this is very useful and very lovely as you can see