9- Running Code on Completion
Jul 03, 2026 23:10
· 4:04
· English
· Whisper Turbo
· 2 Выступающие
Этот протокол истекает сегодня.
Модернизация для постоянного хранения →
Только показываю
0:03
S…
Speaker 2 (9- Running Code on Completion)
Quite often we need to execute some code when an asynchronous operation completes.
0:07
S…
Speaker 1 (9- Running Code on Completion)
For example,
0:08
S…
Speaker 2 (9- Running Code on Completion)
we may want to display the result or store it in the database.
0:12
S…
Speaker 1 (9- Running Code on Completion)
Let me show you how to do that.
0:13
S…
Speaker 1 (9- Running Code on Completion)
First,
0:14
S…
Speaker 2 (9- Running Code on Completion)
let's create a completable future object.
0:16
S…
Speaker 2 (9- Running Code on Completion)
Here we call the supply async method and pass a
0:21
S…
Speaker 2 (9- Running Code on Completion)
lambda that returns one.
0:22
S…
Speaker 2 (9- Running Code on Completion)
Now this returns a completable future object.
0:25
S…
Speaker 2 (9- Running Code on Completion)
So let's store it over here.
0:28
S…
Speaker 2 (9- Running Code on Completion)
Now here we have a method called then run
0:33
S…
Speaker 2 (9- Running Code on Completion)
This is one of those methods that is provided by the completion stage interface.
0:37
S…
Speaker 2 (9- Running Code on Completion)
So if you type completion stage x and
0:41
S…
Speaker 2 (9- Running Code on Completion)
type x dot,
0:42
S…
Speaker 2 (9- Running Code on Completion)
here we can see all the methods declared in this completion stage interface.
0:47
S…
Speaker 2 (9- Running Code on Completion)
Earlier I told you that this interface represents a stage or a step
0:51
S…
Speaker 2 (9- Running Code on Completion)
in an asynchronous operation.
0:53
S…
Speaker 2 (9- Running Code on Completion)
So here we have a bunch of methods for modeling complex asynchronous
0:57
S…
Speaker 2 (9- Running Code on Completion)
operations.
0:58
S…
Speaker 2 (9- Running Code on Completion)
A lot of these methods start with then.
1:00
S…
Speaker 2 (9- Running Code on Completion)
That means when this task is finished,
1:03
S…
Speaker 2 (9- Running Code on Completion)
then do something else.
1:05
S…
Speaker 2 (9- Running Code on Completion)
So here we're going to call future dot then run.
1:08
S…
Speaker 2 (9- Running Code on Completion)
That means we're going to run some code once this task is finished.
1:12
S…
Speaker 2 (9- Running Code on Completion)
Now here we need to pass a runnable object.
1:16
S…
Speaker 2 (9- Running Code on Completion)
So we pass a lambda and print a
1:20
S…
Speaker 2 (9- Running Code on Completion)
message like done.
1:21
S…
Speaker 2 (9- Running Code on Completion)
Let's run our program.
1:25
S…
Speaker 2 (9- Running Code on Completion)
Here's our message.
1:25
S…
Speaker 2 (9- Running Code on Completion)
We also have then run async.
1:29
S…
Speaker 2 (9- Running Code on Completion)
This method is like the run async method that you saw earlier.
1:33
S…
Speaker 2 (9- Running Code on Completion)
It's basically saying run or execute this task in an asynchronous
1:37
S…
Speaker 2 (9- Running Code on Completion)
fashion.
1:37
S…
Speaker 2 (9- Running Code on Completion)
So the task that we pass here is runable object is going to be submitted
1:41
S…
Speaker 2 (9- Running Code on Completion)
to the underlying executor and execute it asynchronously.
1:45
S…
Speaker 1 (9- Running Code on Completion)
Let me show you.
1:46
S…
Speaker 2 (9- Running Code on Completion)
So I'm going to change this lambda.
1:48
S…
Speaker 2 (9- Running Code on Completion)
Let's add a code block.
1:51
S…
Speaker 2 (9- Running Code on Completion)
Here we print our done message.
1:54
S…
Speaker 2 (9- Running Code on Completion)
But before that,
1:55
S…
Speaker 2 (9- Running Code on Completion)
let's print the name of the current thread.
1:57
S…
Speaker 2 (9- Running Code on Completion)
So thread dot current thread dot get name.
2:01
S…
Speaker 2 (9- Running Code on Completion)
Now let's run our program.
2:04
S…
Speaker 2 (9- Running Code on Completion)
You can see that this task was executed on
2:08
S…
Speaker 2 (9- Running Code on Completion)
this thread fork join pool dot common pool worker three.
2:12
S…
Speaker 2 (9- Running Code on Completion)
In contrast,
2:13
S…
Speaker 2 (9- Running Code on Completion)
if we call then run,
2:15
S…
Speaker 1 (9- Running Code on Completion)
this task
2:20
S…
Speaker 2 (9- Running Code on Completion)
is executed on our main thread.
2:22
S…
Speaker 2 (9- Running Code on Completion)
So that is the difference between then run and then run
2:26
S…
Speaker 1 (9- Running Code on Completion)
async.
2:27
S…
Speaker 2 (9- Running Code on Completion)
Now we also have another pair of methods called then accept.
2:31
S…
Speaker 2 (9- Running Code on Completion)
This is useful if you want to get the result of this completable future.
2:35
S…
Speaker 1 (9- Running Code on Completion)
Let me show you.
2:36
S…
Speaker 1 (9- Running Code on Completion)
So delete.
2:37
S…
Speaker 2 (9- Running Code on Completion)
Look at the signature of this method.
2:39
S…
Speaker 2 (9- Running Code on Completion)
Here we should pass a consumer object.
2:42
S…
Speaker 2 (9- Running Code on Completion)
We talked about consumers earlier in the course.
2:44
S…
Speaker 2 (9- Running Code on Completion)
So this consumer interface has a single abstract method
2:49
S…
Speaker 2 (9- Running Code on Completion)
called accept.
2:50
S…
Speaker 2 (9- Running Code on Completion)
It takes an object and doesn't return a value.
2:52
S…
Speaker 2 (9- Running Code on Completion)
So here we can pass a lambda
2:57
S…
Speaker 1 (9- Running Code on Completion)
like this,
2:57
S…
Speaker 2 (9- Running Code on Completion)
result, and in the body of this lambda,
3:01
S…
Speaker 2 (9- Running Code on Completion)
we're going to print the current thread,
3:03
S…
Speaker 2 (9- Running Code on Completion)
so thread dot current thread dot get name,
3:05
S…
Speaker 2 (9- Running Code on Completion)
and then we're going to print the result.
3:08
S…
Speaker 1 (9- Running Code on Completion)
Let's see what happens.
3:10
S…
Speaker 1 (9- Running Code on Completion)
So,
3:13
S…
Speaker 2 (9- Running Code on Completion)
this task was executed on the main thread,
3:15
S…
Speaker 2 (9- Running Code on Completion)
and here's the result of this task.
3:17
S…
Speaker 2 (9- Running Code on Completion)
Now similarly we have then accept async,
3:21
S…
Speaker 2 (9- Running Code on Completion)
and this will submit this task to be executed asynchronously.
3:26
S…
Speaker 1 (9- Running Code on Completion)
Take a look.
3:28
S…
Speaker 2 (9- Running Code on Completion)
Now this task was executed on this thread,
3:31
S…
Speaker 2 (9- Running Code on Completion)
but we don't see the result.
3:33
S…
Speaker 2 (9- Running Code on Completion)
This happened because our main thread finished earlier than this other thread.
3:37
S…
Speaker 2 (9- Running Code on Completion)
So if we put a delay in our main thread,
3:40
S…
Speaker 1 (9- Running Code on Completion)
we can see the result.
3:42
S…
Speaker 2 (9- Running Code on Completion)
Let's put this in sleep for,
3:44
S…
Speaker 1 (9- Running Code on Completion)
let's say,
3:44
S…
Speaker 2 (9- Running Code on Completion)
two seconds and run the program
3:48
S…
Speaker 1 (9- Running Code on Completion)
again. There you go.
3:52
S…
Speaker 2 (9- Running Code on Completion)
So here's our thread and here's the result.
3:54
S…
Speaker 2 (9- Running Code on Completion)
So this is how we can execute some code
3:59
S…
Speaker 2 (9- Running Code on Completion)
upon completion of an asynchronous task.
Эта запись была составлена АИ (автоматическое распознавание речи). Политика МА
Резюме
Нажмите Нажмите Обобщение для составления резюме этой стенограммы.
Резюмируя...
Спросите AI об этом писце
Спросите что - нибудь об этой стенограмме — МА найдет соответствующие разделы и ответ.