8- Implementing an Asynchronous API
Jul 03, 2026 23:07
· 4:18
· English
· Whisper Turbo
· 2 Altofalantes
Esta transcrición caduca hoxe.
Actualizar para almacenamento permanente →
Só mostrando
0:03
S…
Speaker 1 (8- Implementing an Asynchronous API)
Let me show you how to create an asynchronous API.
0:06
S…
Speaker 1 (8- Implementing an Asynchronous API)
This is a very powerful technique that you should start using in your applications right
0:10
S…
Speaker 1 (8- Implementing an Asynchronous API)
away. So in this project,
0:12
S…
Speaker 1 (8- Implementing an Asynchronous API)
I'm going to add a new class mail service.
0:16
S…
Speaker 1 (8- Implementing an Asynchronous API)
We're going to use this class to send emails to our users.
0:19
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now,
0:20
S…
Speaker 1 (8- Implementing an Asynchronous API)
here we need a method like public void send.
0:24
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now in a real application,
0:25
S…
Speaker 1 (8- Implementing an Asynchronous API)
we should pass a mail message object,
0:27
S…
Speaker 1 (8- Implementing an Asynchronous API)
but let's not worry about that.
0:29
S…
Speaker 1 (8- Implementing an Asynchronous API)
Instead,
0:30
S…
Speaker 1 (8- Implementing an Asynchronous API)
we're just going to print a message like mail was sent.
0:34
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now, sending an email is a long running operation because we have to go over
0:38
S…
Speaker 1 (8- Implementing an Asynchronous API)
the network.
0:39
S…
Speaker 1 (8- Implementing an Asynchronous API)
Basically,
0:40
S…
Speaker 1 (8- Implementing an Asynchronous API)
anytime we have to touch the file system or the network,
0:43
S…
Speaker 1 (8- Implementing an Asynchronous API)
we're dealing with a long running operation.
0:45
S…
Speaker 1 (8- Implementing an Asynchronous API)
And we shouldn't run these operations on the main thread.
0:48
S…
Speaker 1 (8- Implementing an Asynchronous API)
We should run them on a separate thread.
0:50
S…
Speaker 1 (8- Implementing an Asynchronous API)
So to simulate a long running operation,
0:53
S…
Speaker 1 (8- Implementing an Asynchronous API)
I'm going to use our long task class that we created in the last section.
0:57
S…
Speaker 1 (8- Implementing an Asynchronous API)
Here we call the simulate method,
0:59
S…
Speaker 1 (8- Implementing an Asynchronous API)
which adds a three second delay to this method.
1:02
S…
Speaker 1 (8- Implementing an Asynchronous API)
Okay.
1:02
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now,
1:04
S…
Speaker 1 (8- Implementing an Asynchronous API)
In our main method,
1:05
S…
Speaker 1 (8- Implementing an Asynchronous API)
I'm going to create a new mail service
1:09
S…
Speaker 1 (8- Implementing an Asynchronous API)
and then call service .send.
1:12
S…
Speaker 2 (8- Implementing an Asynchronous API)
Now,
1:13
S…
Speaker 1 (8- Implementing an Asynchronous API)
this is a synchronous or blocking operation.
1:16
S…
Speaker 1 (8- Implementing an Asynchronous API)
So if we print another message here like hello world,
1:20
S…
Speaker 1 (8- Implementing an Asynchronous API)
we're not going to see this until this method returns.
1:23
S…
Speaker 1 (8- Implementing an Asynchronous API)
Let me show you.
1:24
S…
Speaker 1 (8- Implementing an Asynchronous API)
So run,
1:25
S…
Speaker 1 (8- Implementing an Asynchronous API)
you're waiting for the mail to be sent.
1:28
S…
Speaker 1 (8- Implementing an Asynchronous API)
Three seconds later,
1:29
S…
Speaker 1 (8- Implementing an Asynchronous API)
the mail is sent and now we see the hello world message.
1:32
S…
Speaker 1 (8- Implementing an Asynchronous API)
So this is the problem with synchronous or blocking code.
1:36
S…
Speaker 2 (8- Implementing an Asynchronous API)
Now,
1:36
S…
Speaker 1 (8- Implementing an Asynchronous API)
anytime we have a long running operation like querying a database,
1:40
S…
Speaker 1 (8- Implementing an Asynchronous API)
calling a remote service,
1:41
S…
Speaker 1 (8- Implementing an Asynchronous API)
working with the file system,
1:43
S…
Speaker 1 (8- Implementing an Asynchronous API)
we should run these operations asynchronously.
1:46
S…
Speaker 1 (8- Implementing an Asynchronous API)
Let me show you how to do that.
1:47
S…
Speaker 1 (8- Implementing an Asynchronous API)
So we go back to our mail service class.
1:50
S…
Speaker 1 (8- Implementing an Asynchronous API)
I'm going to re -implement this method in an asynchronous fashion.
1:54
S…
Speaker 1 (8- Implementing an Asynchronous API)
So we add another method,
1:56
S…
Speaker 1 (8- Implementing an Asynchronous API)
public,
1:56
S…
Speaker 1 (8- Implementing an Asynchronous API)
instead of returning void,
1:58
S…
Speaker 1 (8- Implementing an Asynchronous API)
we're going to return a completable future of void.
2:02
S…
Speaker 1 (8- Implementing an Asynchronous API)
If this method returned an integer here,
2:05
S…
Speaker 1 (8- Implementing an Asynchronous API)
we would use completable future of integer.
2:07
S…
Speaker 1 (8- Implementing an Asynchronous API)
Okay.
2:08
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now we call this method send async.
2:11
S…
Speaker 1 (8- Implementing an Asynchronous API)
So by convention,
2:12
S…
Speaker 1 (8- Implementing an Asynchronous API)
we add the async suffix to this method.
2:15
S…
Speaker 1 (8- Implementing an Asynchronous API)
Okay.
2:15
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now here we should return a completable future object.
2:20
S…
Speaker 1 (8- Implementing an Asynchronous API)
You learned how to do that in the last video.
2:21
S…
Speaker 1 (8- Implementing an Asynchronous API)
So we return completable future because
2:25
S…
Speaker 1 (8- Implementing an Asynchronous API)
we're not going to return a value.
2:27
S…
Speaker 1 (8- Implementing an Asynchronous API)
We should call the run async method.
2:30
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now here we should pass a runnable object.
2:32
S…
Speaker 1 (8- Implementing an Asynchronous API)
So we use a lambda and in the body of this lambda
2:36
S…
Speaker 1 (8- Implementing an Asynchronous API)
we simply call our synchronous method as simple
2:41
S…
Speaker 1 (8- Implementing an Asynchronous API)
as that.
2:41
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now with this single line of code we can execute this method in
2:45
S…
Speaker 1 (8- Implementing an Asynchronous API)
an asynchronous fashion because when we call the run async method
2:49
S…
Speaker 1 (8- Implementing an Asynchronous API)
the task that we pass here is going to be executed on a separate thread
2:54
S…
Speaker 1 (8- Implementing an Asynchronous API)
on the common thread pool.
2:55
S…
Speaker 1 (8- Implementing an Asynchronous API)
So if you have an existing synchronous method,
2:58
S…
Speaker 1 (8- Implementing an Asynchronous API)
you can simply convert it to an asynchronous method by wrapping it inside
3:02
S…
Speaker 1 (8- Implementing an Asynchronous API)
a completable future object.
3:04
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now,
3:05
S…
Speaker 1 (8- Implementing an Asynchronous API)
back to our main method.
3:07
S…
Speaker 1 (8- Implementing an Asynchronous API)
Here we call send async.
3:10
S…
Speaker 2 (8- Implementing an Asynchronous API)
Now,
3:10
S…
Speaker 1 (8- Implementing an Asynchronous API)
because this is an asynchronous method,
3:12
S…
Speaker 1 (8- Implementing an Asynchronous API)
it's not going to block the current thread.
3:14
S…
Speaker 1 (8- Implementing an Asynchronous API)
So the moment I run this program,
3:16
S…
Speaker 1 (8- Implementing an Asynchronous API)
we're going to see this hello world message immediately.
3:18
S…
Speaker 1 (8- Implementing an Asynchronous API)
Then we wait three seconds for the mail to be sent.
3:21
S…
Speaker 1 (8- Implementing an Asynchronous API)
Take a look.
3:22
S…
Speaker 1 (8- Implementing an Asynchronous API)
So run
3:24
S…
Speaker 1 (8- Implementing an Asynchronous API)
We see the hello world message,
3:25
S…
Speaker 1 (8- Implementing an Asynchronous API)
but what happened to our mail?
3:26
S…
Speaker 1 (8- Implementing an Asynchronous API)
Well,
3:27
S…
Speaker 1 (8- Implementing an Asynchronous API)
here we have a command line program and this program terminated so quickly
3:31
S…
Speaker 1 (8- Implementing an Asynchronous API)
that we didn't see the result of this task that was executed on a separate thread.
3:35
S…
Speaker 1 (8- Implementing an Asynchronous API)
We don't have this problem in a mobile or a desktop app because these apps
3:39
S…
Speaker 1 (8- Implementing an Asynchronous API)
are continuously running until the user terminates them.
3:42
S…
Speaker 1 (8- Implementing an Asynchronous API)
So to prove to you that our mail was being sent on a separate thread,
3:46
S…
Speaker 1 (8- Implementing an Asynchronous API)
I'm going to put a delay over here.
3:49
S…
Speaker 1 (8- Implementing an Asynchronous API)
Let's say thread that sleep for five seconds.
3:53
S…
Speaker 1 (8- Implementing an Asynchronous API)
we should wrap this in a try catch block.
3:55
S…
Speaker 1 (8- Implementing an Asynchronous API)
Now we run our program so we
4:00
S…
Speaker 1 (8- Implementing an Asynchronous API)
see the hello world message three seconds later the mail was sent and then two seconds
4:05
S…
Speaker 1 (8- Implementing an Asynchronous API)
later our program finished.
4:06
S…
Speaker 1 (8- Implementing an Asynchronous API)
So this is the benefit of asynchronous API's they
4:10
S…
Speaker 1 (8- Implementing an Asynchronous API)
don't block the current thread and allow us to better exploit our parallel
4:15
S…
Speaker 1 (8- Implementing an Asynchronous API)
hardware.
Esta transcrición foi xerada por IA (recoñecemento automático de fala). Pode conter erros; verifique co son orixinal para uso crítico. Política de IA
Resumo
Prema Resumo para xerar un resumo de IA desta transcrición.
A resumir...
Preguntarlle á IA sobre esta transcrición
Pregunte calquera cousa acerca desta transcrición — a IA atopará as seccións relevantes e responderá.