ਕੇਵਲ ਵੇਖਾਓ
0:03
S… Speaker 1 (12- Composing Completable Futures)
Quite often we want to start a task upon completion of another task.
0:07
S… Speaker 1 (12- Composing Completable Futures)
For example,
0:08
S… Speaker 1 (12- Composing Completable Futures)
let's say we have the idea of a user Now we want to get the user's email
0:12
S… Speaker 1 (12- Composing Completable Futures)
So we have to go to our database and read that user record with this
0:16
S… Speaker 1 (12- Composing Completable Futures)
we can get their email Now we want to pass this email to one of those
0:20
S… Speaker 1 (12- Composing Completable Futures)
music streaming websites where people have their own playlist So we pass
0:25
S… Speaker 1 (12- Composing Completable Futures)
the email and get the playlist for given person
0:28
S… Speaker 1 (12- Composing Completable Futures)
So here we have two tasks,
0:30
S… Speaker 1 (12- Composing Completable Futures)
two asynchronous operations.
0:31
S… Speaker 1 (12- Composing Completable Futures)
We want to start the second task upon completion of the first task.
0:35
S… Speaker 1 (12- Composing Completable Futures)
Completable futures make it really easy to implement these kind
0:39
S… Speaker 1 (12- Composing Completable Futures)
of scenarios in a declarative and efficient way.
0:42
S… Speaker 1 (12- Composing Completable Futures)
Let me show you.
0:43
S… Speaker 1 (12- Composing Completable Futures)
So first we're going to create a completable future for getting the email
0:47
S… Speaker 1 (12- Composing Completable Futures)
of a user.
0:48
S… Speaker 1 (12- Composing Completable Futures)
So completable future,
0:50
S… Speaker 1 (12- Composing Completable Futures)
let's apply async to pass a lambda
0:53
S… Speaker 1 (12- Composing Completable Futures)
And here we just want to return the email for this user.
0:57
S… Speaker 1 (12- Composing Completable Futures)
In a real world application,
0:58
S… Speaker 1 (12- Composing Completable Futures)
in the body of this lambda,
1:00
S… Speaker 1 (12- Composing Completable Futures)
we're going to query a database.
1:01
S… Speaker 1 (12- Composing Completable Futures)
But here I just want to return a string.
1:03
S… Speaker 2 (12- Composing Completable Futures)
Now,
1:04
S… Speaker 1 (12- Composing Completable Futures)
this returns a completable future of string,
1:09
S… Speaker 1 (12- Composing Completable Futures)
right?
1:09
S… Speaker 1 (12- Composing Completable Futures)
So completable future of string.
1:11
S… Speaker 2 (12- Composing Completable Futures)
Now,
1:12
S… Speaker 1 (12- Composing Completable Futures)
we want to start the second task upon completion of the
1:16
S… Speaker 1 (12- Composing Completable Futures)
first task.
1:17
S… Speaker 1 (12- Composing Completable Futures)
To do that,
1:18
S… Speaker 1 (12- Composing Completable Futures)
we call future dot then compose.
1:23
S… Speaker 1 (12- Composing Completable Futures)
Now here we should pass a function that maps a string to a completion
1:27
S… Speaker 1 (12- Composing Completable Futures)
stage object.
1:28
S… Speaker 1 (12- Composing Completable Futures)
That means from this function we should return a new completable
1:32
S… Speaker 1 (12- Composing Completable Futures)
future.
1:32
S… Speaker 1 (12- Composing Completable Futures)
That completable future represents a new asynchronous task,
1:37
S… Speaker 1 (12- Composing Completable Futures)
right?
1:37
S… Speaker 1 (12- Composing Completable Futures)
So what is the string that we get here?
1:40
S… Speaker 1 (12- Composing Completable Futures)
That is the email that is returned from our first task.
1:44
S… Speaker 1 (12- Composing Completable Futures)
So we get the email,
1:45
S… Speaker 1 (12- Composing Completable Futures)
now we want to pass this to a second task.
1:48
S… Speaker 1 (12- Composing Completable Futures)
So here we should return a new completable future object.
1:53
S… Speaker 1 (12- Composing Completable Futures)
That is very easy.
1:53
S… Speaker 1 (12- Composing Completable Futures)
So completable future dot supply async.
1:57
S… Speaker 1 (12- Composing Completable Futures)
Here we should call that remote service to get the playlist for this user.
2:01
S… Speaker 1 (12- Composing Completable Futures)
So we pass another supplier object.
2:05
S… Speaker 1 (12- Composing Completable Futures)
With this supplier,
2:07
S… Speaker 1 (12- Composing Completable Futures)
I'm going to return this string,
2:09
S… Speaker 1 (12- Composing Completable Futures)
playlist.
2:09
S… Speaker 1 (12- Composing Completable Futures)
Now in real application,
2:11
S… Speaker 1 (12- Composing Completable Futures)
we're going to return a playlist object or a list of songs.
2:15
S… Speaker 1 (12- Composing Completable Futures)
But here I don't want to waste time creating different classes.
2:18
S… Speaker 1 (12- Composing Completable Futures)
I just want to return a string.
2:20
S… Speaker 1 (12- Composing Completable Futures)
All I want you to understand is that our first task returns an email,
2:24
S… Speaker 1 (12- Composing Completable Futures)
then we pass the email to our second task and get the
2:28
S… Speaker 1 (12- Composing Completable Futures)
playlist, okay?
2:29
S… Speaker 1 (12- Composing Completable Futures)
So when we call then
2:33
S… Speaker 1 (12- Composing Completable Futures)
compose, we get a new completable future,
2:36
S… Speaker 1 (12- Composing Completable Futures)
so here we can chain another method like then accept,
2:40
S… Speaker 1 (12- Composing Completable Futures)
we get the playlist and print it on the terminal.
2:44
S… Speaker 1 (12- Composing Completable Futures)
As simple as that.
2:47
S… Speaker 1 (12- Composing Completable Futures)
Now, let's reformat our code.
2:49
S… Speaker 1 (12- Composing Completable Futures)
So let's put each method call on a new line.
2:52
S… Speaker 1 (12- Composing Completable Futures)
That is better.
2:53
S… Speaker 1 (12- Composing Completable Futures)
We don't need the comments.
2:55
S… Speaker 1 (12- Composing Completable Futures)
Let's run our program.
2:57
S… Speaker 1 (12- Composing Completable Futures)
So we get the playlist.
3:00
S… Speaker 1 (12- Composing Completable Futures)
Beautiful.
3:00
S… Speaker 2 (12- Composing Completable Futures)
Now,
3:01
S… Speaker 1 (12- Composing Completable Futures)
let's improve this code further.
3:03
S… Speaker 1 (12- Composing Completable Futures)
Technically we don't need this future variable.
3:06
S… Speaker 1 (12- Composing Completable Futures)
We can immediately call this method on the object that
3:10
S… Speaker 1 (12- Composing Completable Futures)
is returned over here.
3:12
S… Speaker 1 (12- Composing Completable Futures)
So let's get rid of the future variable.
3:16
S… Speaker 1 (12- Composing Completable Futures)
So we call completable future the supply async.
3:20
S… Speaker 1 (12- Composing Completable Futures)
This is better,
3:21
S… Speaker 1 (12- Composing Completable Futures)
but we can make this even better.
3:23
S… Speaker 1 (12- Composing Completable Futures)
What is this lambda representing?
3:25
S… Speaker 1 (12- Composing Completable Futures)
It's representing a task for getting a user object from a database.
3:29
S… Speaker 1 (12- Composing Completable Futures)
So in a real application,
3:31
S… Speaker 1 (12- Composing Completable Futures)
we would probably have a method somewhere like public,
3:34
S… Speaker 1 (12- Composing Completable Futures)
in this case static.
3:36
S… Speaker 1 (12- Composing Completable Futures)
In a real application,
3:38
S… Speaker 1 (12- Composing Completable Futures)
we would return a user object,
3:39
S… Speaker 1 (12- Composing Completable Futures)
but here I want to return a string.
3:41
S… Speaker 1 (12- Composing Completable Futures)
We call this method getUserEmail.
3:45
S… Speaker 1 (12- Composing Completable Futures)
I told you that querying a database is a long -running operation.
3:49
S… Speaker 1 (12- Composing Completable Futures)
So we should make our APIs asynchronous.
3:51
S… Speaker 1 (12- Composing Completable Futures)
So instead of returning a string,
3:53
S… Speaker 1 (12- Composing Completable Futures)
we should return a completable future of string.
3:58
S… Speaker 2 (12- Composing Completable Futures)
Now,
3:59
S… Speaker 1 (12- Composing Completable Futures)
inside this method,
4:00
S… Speaker 1 (12- Composing Completable Futures)
we're going to have some code for querying a database and getting someone's email.
4:04
S… Speaker 1 (12- Composing Completable Futures)
So we return completable future dot supply
4:08
S… Speaker 1 (12- Composing Completable Futures)
async and return this email.
4:11
S… Speaker 2 (12- Composing Completable Futures)
Okay?
4:13
S… Speaker 1 (12- Composing Completable Futures)
Now, with this method,
4:14
S… Speaker 1 (12- Composing Completable Futures)
we no longer need this piece of code over here.
4:16
S… Speaker 1 (12- Composing Completable Futures)
We can simply call get user email.
4:20
S… Speaker 2 (12- Composing Completable Futures)
Also,
4:21
S… Speaker 1 (12- Composing Completable Futures)
as I told you before,
4:22
S… Speaker 1 (12- Composing Completable Futures)
it's better to use the async suffix here to indicate that
4:26
S… Speaker 1 (12- Composing Completable Futures)
this is an asynchronous method.
4:27
S… Speaker 1 (12- Composing Completable Futures)
So let's rename this to get user email async.
4:32
S… Speaker 2 (12- Composing Completable Futures)
Now,
4:33
S… Speaker 1 (12- Composing Completable Futures)
we're going to apply the same technique over here.
4:38
S… Speaker 1 (12- Composing Completable Futures)
for the second task that gets an email and returns a
4:42
S… Speaker 1 (12- Composing Completable Futures)
playlist asynchronously so in a real application somewhere we're going to
4:46
S… Speaker 1 (12- Composing Completable Futures)
have another method for getting the playlist of a given user so
4:51
S… Speaker 1 (12- Composing Completable Futures)
public static we have a long -running operation so
4:55
S… Speaker 1 (12- Composing Completable Futures)
we should return a completable future of in this case string
5:00
S… Speaker 1 (12- Composing Completable Futures)
and we call that method get playlist async
5:05
S… Speaker 1 (12- Composing Completable Futures)
Now here we need a parameter of type string called email.
5:08
S… Speaker 1 (12- Composing Completable Futures)
So this method gets the email of a user and calls
5:12
S… Speaker 1 (12- Composing Completable Futures)
a remote API to get the playlist for that user in an asynchronous fashion.
5:17
S… Speaker 1 (12- Composing Completable Futures)
Now here we should return a completable feature that's
5:21
S… Speaker 1 (12- Composing Completable Futures)
a play sync and we return this playlist.
5:25
S… Speaker 2 (12- Composing Completable Futures)
Right?
5:26
S… Speaker 1 (12- Composing Completable Futures)
Now with this refactoring we no longer need this
5:30
S… Speaker 1 (12- Composing Completable Futures)
lambda over here.
5:32
S… Speaker 1 (12- Composing Completable Futures)
We can use a method reference.
5:33
S… Speaker 1 (12- Composing Completable Futures)
So the name of this class is completable futures demo.
5:37
S… Speaker 1 (12- Composing Completable Futures)
I'm going to type that here.
5:39
S… Speaker 1 (12- Composing Completable Futures)
Completable futures demo.
5:41
S… Speaker 1 (12- Composing Completable Futures)
Then we have colon colon and finally get playlist
5:45
S… Speaker 1 (12- Composing Completable Futures)
async.
5:46
S… Speaker 1 (12- Composing Completable Futures)
Now look at the end result.
5:47
S… Speaker 1 (12- Composing Completable Futures)
We're getting the email of a user in an asynchronous fashion.
5:51
S… Speaker 1 (12- Composing Completable Futures)
Then we're composing this with another operation for getting the playlist
5:55
S… Speaker 1 (12- Composing Completable Futures)
and finally we print the playlist.
5:58
S… Speaker 1 (12- Composing Completable Futures)
This is the beauty of completable features.
6:00
S… Speaker 1 (12- Composing Completable Futures)
We can build this kind of complex asynchronous operations in
6:04
S… Speaker 1 (12- Composing Completable Futures)
a declarative way.

ਇਹ ਟਰਾਂਸਕਰੀਪਟ AI (ਆਟੋਮੈਟਿਕ ਬੋਲੀ ਪਛਾਣ) ਵਲੋਂ ਬਣਾਈ ਗਈ ਹੈ । ਇਸ ਵਿੱਚ ਗਲਤੀਆਂ ਹੋ ਸਕਦੀਆਂ ਹਨ - ਗੰਭੀਰ ਵਰਤੋਂ ਲਈ ਅਸਲੀ ਆਡੀਓ ਨਾਲ ਜਾਂਚ ਕਰੋ । AI ਪਾਲਸੀ

❤️ STT.ai ਪਸੰਦ ਹੈ? ਆਪਣੇ ਦੋਸਤਾਂ ਨੂੰ ਦੱਸੋ!
ਸੰਖੇਪ
ਇਸ ਟਰਾਂਸਕਰੀਪਟ ਦਾ AI ਸੰਖੇਪ ਬਣਾਉਣ ਲਈ ਸੰਖੇਪ ਕਲਿੱਕ ਕਰੋ ।
ਸੰਖੇਪ...
ਇਹ ਟਰਾਂਸਕਰੀਪਟ ਬਾਰੇ AI ਨੂੰ ਪੁੱਛੋ
ਇਸ ਟਰਾਂਸਕਰੀਪਟ ਬਾਰੇ ਕੁਝ ਪੁੱਛੋ — AI ਸਬੰਧਤ ਭਾਗ ਲੱਭੇਗਾ ਅਤੇ ਜਵਾਬ ਦੇਵੇਗਾ।