13- Combining Completable Futures
Jul 04, 2026 01:15
· 4:32
· English
· Whisper Turbo
· 1 Cynhadledd
Mae'r trosglwyddiad hwn yn darfod heddiw.
Uwchraddio i storfa barhaol →
Dangos yn Unig
0:03
S…
Speaker 1 (13- Combining Completable Futures)
In my opinion,
0:04
S…
Speaker 1 (13- Combining Completable Futures)
one of the most powerful features of completable futures is the ability
0:08
S…
Speaker 1 (13- Combining Completable Futures)
to start two tasks asynchronously and then combine the results.
0:13
S…
Speaker 1 (13- Combining Completable Futures)
For example,
0:13
S…
Speaker 1 (13- Combining Completable Futures)
let's say we call a remote service to get the price of a
0:17
S…
Speaker 1 (13- Combining Completable Futures)
product and that service returns the price in US dollars.
0:21
S…
Speaker 1 (13- Combining Completable Futures)
So we get 20 US dollars.
0:23
S…
Speaker 1 (13- Combining Completable Futures)
Now at the same time,
0:25
S…
Speaker 1 (13- Combining Completable Futures)
in parallel,
0:26
S…
Speaker 1 (13- Combining Completable Futures)
we want to call another service to get the exchange rate between US
0:30
S…
Speaker 1 (13- Combining Completable Futures)
dollars and euros.
0:32
S…
Speaker 1 (13- Combining Completable Futures)
So that service is going to return 0 .9.
0:34
S…
Speaker 1 (13- Combining Completable Futures)
Now we don't want to call the second service upon completion of the first
0:38
S…
Speaker 1 (13- Combining Completable Futures)
task.
0:38
S…
Speaker 1 (13- Combining Completable Futures)
We want to start this task concurrently and then wait for both of them to complete
0:43
S…
Speaker 1 (13- Combining Completable Futures)
in order to calculate the result.
0:45
S…
Speaker 1 (13- Combining Completable Futures)
So let me show you how to do this.
0:47
S…
Speaker 1 (13- Combining Completable Futures)
First we want to create a completable future to return the price of
0:51
S…
Speaker 1 (13- Combining Completable Futures)
a product.
0:51
S…
Speaker 1 (13- Combining Completable Futures)
So completable future .supply async and
0:56
S…
Speaker 1 (13- Combining Completable Futures)
here we return 20.
0:57
S…
Speaker 1 (13- Combining Completable Futures)
That is our first task.
1:01
S…
Speaker 1 (13- Combining Completable Futures)
Now, in our second task,
1:02
S…
Speaker 1 (13- Combining Completable Futures)
we're going to call another service to get the exchange rate between US
1:06
S…
Speaker 1 (13- Combining Completable Futures)
dollars and euros.
1:07
S…
Speaker 1 (13- Combining Completable Futures)
So one more time,
1:08
S…
Speaker 1 (13- Combining Completable Futures)
completable future,
1:10
S…
Speaker 1 (13- Combining Completable Futures)
dot supply async,
1:11
S…
Speaker 1 (13- Combining Completable Futures)
here we return 0 .9.
1:14
S…
Speaker 1 (13- Combining Completable Futures)
This is our second task.
1:16
S…
Speaker 1 (13- Combining Completable Futures)
Now we call first dot then combine.
1:22
S…
Speaker 1 (13- Combining Completable Futures)
With this we can combine the result of two asynchronous operations.
1:26
S…
Speaker 1 (13- Combining Completable Futures)
So we can wait for both of them to complete to calculate the final result.
1:30
S…
Speaker 1 (13- Combining Completable Futures)
But here's the interesting part.
1:32
S…
Speaker 1 (13- Combining Completable Futures)
Calling this method is not going to block the current thread.
1:35
S…
Speaker 1 (13- Combining Completable Futures)
We're basically building a processing pipeline.
1:38
S…
Speaker 1 (13- Combining Completable Futures)
We're building a recipe.
1:39
S…
Speaker 1 (13- Combining Completable Futures)
We're telling the executor framework,
1:41
S…
Speaker 1 (13- Combining Completable Futures)
hey, go ahead and start these two tasks concurrently.
1:45
S…
Speaker 1 (13- Combining Completable Futures)
When they're both complete,
1:46
S…
Speaker 1 (13- Combining Completable Futures)
start a new task to do something else.
1:49
S…
Speaker 1 (13- Combining Completable Futures)
All of this is happening asynchronously,
1:51
S…
Speaker 1 (13- Combining Completable Futures)
okay?
1:52
S…
Speaker 1 (13- Combining Completable Futures)
So look at the signature of this method.
1:54
S…
Speaker 1 (13- Combining Completable Futures)
Here we have two parameters.
1:56
S…
Speaker 1 (13- Combining Completable Futures)
The first parameter is a completion stage object.
2:00
S…
Speaker 1 (13- Combining Completable Futures)
That is our second task.
2:01
S…
Speaker 1 (13- Combining Completable Futures)
So let's pass it over here.
2:03
S…
Speaker 1 (13- Combining Completable Futures)
Now the second parameter is a bifunction.
2:06
S…
Speaker 1 (13- Combining Completable Futures)
That is a function with two parameters that returns a value.
2:10
S…
Speaker 1 (13- Combining Completable Futures)
Look at the type of these parameters.
2:12
S…
Speaker 1 (13- Combining Completable Futures)
The first parameter is an integer.
2:14
S…
Speaker 1 (13- Combining Completable Futures)
The second is a double.
2:16
S…
Speaker 1 (13- Combining Completable Futures)
So these parameters represent the results of
2:20
S…
Speaker 1 (13- Combining Completable Futures)
our tasks.
2:21
S…
Speaker 1 (13- Combining Completable Futures)
So the first task returns a price in US dollars and
2:26
S…
Speaker 1 (13- Combining Completable Futures)
the second task returns the exchange rate.
2:28
S…
Speaker 1 (13- Combining Completable Futures)
Exchange rate.
2:31
S…
Speaker 1 (13- Combining Completable Futures)
So this is our buy function.
2:32
S…
Speaker 1 (13- Combining Completable Futures)
Now we get these two values and based on these we're going to calculate
2:37
S…
Speaker 1 (13- Combining Completable Futures)
a new value.
2:38
S…
Speaker 1 (13- Combining Completable Futures)
So here we can return price times exchange rate.
2:42
S…
Speaker 1 (13- Combining Completable Futures)
Okay?
2:43
S…
Speaker 1 (13- Combining Completable Futures)
Now,
2:44
S…
Speaker 1 (13- Combining Completable Futures)
this method
2:45
S…
Speaker 1 (13- Combining Completable Futures)
Then combine is going to return a new completable future.
2:49
S…
Speaker 1 (13- Combining Completable Futures)
So after that we can call then accept.
2:53
S…
Speaker 1 (13- Combining Completable Futures)
Here we get the result and print it.
2:56
S…
Speaker 1 (13- Combining Completable Futures)
Now let's reformat our code.
3:00
S…
Speaker 1 (13- Combining Completable Futures)
That is better.
3:02
S…
Speaker 1 (13- Combining Completable Futures)
So run.
3:04
S…
Speaker 1 (13- Combining Completable Futures)
So the price is $18.
3:07
S…
Speaker 1 (13- Combining Completable Futures)
Now let's make this example more interesting.
3:10
S…
Speaker 1 (13- Combining Completable Futures)
Let's imagine that the first task returns the price as
3:14
S…
Speaker 1 (13- Combining Completable Futures)
a string.
3:15
S…
Speaker 1 (13- Combining Completable Futures)
20 US dollars.
3:18
S…
Speaker 1 (13- Combining Completable Futures)
Now we have a compilation error because we cannot multiply a string by
3:22
S…
Speaker 1 (13- Combining Completable Futures)
a number.
3:23
S…
Speaker 1 (13- Combining Completable Futures)
So in our first task,
3:25
S…
Speaker 1 (13- Combining Completable Futures)
we should transform the result.
3:27
S…
Speaker 1 (13- Combining Completable Futures)
So right after calling the supply async method,
3:30
S…
Speaker 1 (13- Combining Completable Futures)
we call then apply.
3:33
S…
Speaker 1 (13- Combining Completable Futures)
Here we should pass a function that takes a string and returns a
3:37
S…
Speaker 1 (13- Combining Completable Futures)
new value, in this case an integer.
3:39
S…
Speaker 1 (13- Combining Completable Futures)
So I'm going to reformat this code.
3:42
S…
Speaker 1 (13- Combining Completable Futures)
First we call supply async and then
3:46
S…
Speaker 1 (13- Combining Completable Futures)
then apply.
3:47
S…
Speaker 1 (13- Combining Completable Futures)
So here we get a string
3:51
S…
Speaker 1 (13- Combining Completable Futures)
Now we call str .replace.
3:53
S…
Speaker 1 (13- Combining Completable Futures)
I'm going to replace US dollars with an empty string.
3:58
S…
Speaker 1 (13- Combining Completable Futures)
We could also use regular expressions.
4:00
S…
Speaker 1 (13- Combining Completable Futures)
It doesn't really matter.
4:01
S…
Speaker 1 (13- Combining Completable Futures)
This is a very basic transformation.
4:03
S…
Speaker 1 (13- Combining Completable Futures)
So we get that.
4:04
S…
Speaker 1 (13- Combining Completable Futures)
That gives us the price.
4:06
S…
Speaker 1 (13- Combining Completable Futures)
Now we call integer .parseInt
4:10
S…
Speaker 1 (13- Combining Completable Futures)
and pass the price.
4:12
S…
Speaker 1 (13- Combining Completable Futures)
And finally return the result.
4:15
S…
Speaker 1 (13- Combining Completable Futures)
So here's our first task.
4:17
S…
Speaker 1 (13- Combining Completable Futures)
Then we have our second task.
4:18
S…
Speaker 1 (13- Combining Completable Futures)
We combine them and wait for both of them to complete before
4:22
S…
Speaker 1 (13- Combining Completable Futures)
displaying the result.
4:24
S…
Speaker 1 (13- Combining Completable Futures)
Now all of this is happening asynchronously.
4:26
S…
Speaker 1 (13- Combining Completable Futures)
So none of this code is going to block the current threat.
Creuwyd y trawsgrifiad hwn gan AI (cydnabod llais awtomatig). Gall gynnwys gwallau - gwiriwch yn erbyn y sain wreiddiol am ddefnydd pwysig. Polisi AI
Crynodeb
Cliciwch Crynodeb i greu crynodeb AI o' r trosysgrif yma.
Crynodeb...
Gofyn i AI Am Y Trawssgrifiad Yma
Gofynnwch unrhyw beth am y trosglwyddiad hwn — bydd y AI yn canfod adrannau perthnasol ac yn ateb.