16- Handling timeouts
Jul 04, 2026 01:21
· 2:24
· English
· Whisper Turbo
· 2 Выступающие
Этот протокол истекает сегодня.
Модернизация для постоянного хранения →
Только показываю
0:03
S…
Speaker 2 (16- Handling timeouts)
When calling a remote service,
0:05
S…
Speaker 2 (16- Handling timeouts)
you want to have a limit in terms of how long we're willing to wait to get a response.
0:09
S…
Speaker 2 (16- Handling timeouts)
We don't want to wait forever.
0:10
S…
Speaker 2 (16- Handling timeouts)
So let me show you how to implement this scenario.
0:13
S…
Speaker 2 (16- Handling timeouts)
I'm going to create a completable future.
0:15
S…
Speaker 1 (16- Handling timeouts)
Supply async.
0:17
S…
Speaker 1 (16- Handling timeouts)
Now,
0:18
S…
Speaker 2 (16- Handling timeouts)
here we want to simulate a slow connection.
0:21
S…
Speaker 1 (16- Handling timeouts)
So long task that simulate.
0:24
S…
Speaker 2 (16- Handling timeouts)
Eventually we expect to get a result like one.
0:28
S…
Speaker 1 (16- Handling timeouts)
It doesn't really matter what the result is.
0:31
S…
Speaker 1 (16- Handling timeouts)
So this returns a completable future.
0:34
S…
Speaker 1 (16- Handling timeouts)
Now,
0:35
S…
Speaker 2 (16- Handling timeouts)
this future object has a method called or timeout.
0:40
S…
Speaker 1 (16- Handling timeouts)
And with this,
0:41
S…
Speaker 2 (16- Handling timeouts)
we can enforce a limit.
0:42
S…
Speaker 2 (16- Handling timeouts)
Here we need to pass two arguments.
0:45
S…
Speaker 2 (16- Handling timeouts)
One is a timeout value.
0:46
S…
Speaker 1 (16- Handling timeouts)
Let's say one.
0:47
S…
Speaker 2 (16- Handling timeouts)
And the other one is a time unit.
0:49
S…
Speaker 2 (16- Handling timeouts)
This is an enumeration.
0:51
S…
Speaker 2 (16- Handling timeouts)
So here we can say we want to wait one second.
0:54
S…
Speaker 2 (16- Handling timeouts)
Now, this will return a new completable future that we time
0:59
S…
Speaker 2 (16- Handling timeouts)
out after one second.
1:00
S…
Speaker 2 (16- Handling timeouts)
So if we call the get method to get the result,
1:04
S…
Speaker 1 (16- Handling timeouts)
we're going to get an exception.
1:06
S…
Speaker 2 (16- Handling timeouts)
Let's wrap this inside a try catch block.
1:09
S…
Speaker 1 (16- Handling timeouts)
Okay,
1:10
S…
Speaker 2 (16- Handling timeouts)
now we get the result and start over here and finally print
1:15
S…
Speaker 1 (16- Handling timeouts)
it. Let's run the program.
1:20
S…
Speaker 2 (16- Handling timeouts)
So after one second our program crashed we got an exception of type execution
1:24
S…
Speaker 2 (16- Handling timeouts)
exception which is caused by a timeout exception Okay,
1:28
S…
Speaker 2 (16- Handling timeouts)
now this is not an ideal experience for the end user a
1:32
S…
Speaker 2 (16- Handling timeouts)
better approach is to recover with a default value so Instead
1:39
S…
Speaker 2 (16- Handling timeouts)
of calling or timeout.
1:40
S…
Speaker 2 (16- Handling timeouts)
We're going to call complete on timeout
1:44
S…
Speaker 2 (16- Handling timeouts)
Here we need to pass three values.
1:46
S…
Speaker 2 (16- Handling timeouts)
The first one is the default value that we're going to return if this task times
1:50
S…
Speaker 1 (16- Handling timeouts)
out. So we're going to return one.
1:53
S…
Speaker 2 (16- Handling timeouts)
The second is our timeout value.
1:55
S…
Speaker 1 (16- Handling timeouts)
Once again,
1:55
S…
Speaker 2 (16- Handling timeouts)
one and finally a time unit.
1:58
S…
Speaker 2 (16- Handling timeouts)
So time unit dot seconds.
2:01
S…
Speaker 2 (16- Handling timeouts)
So this will return a new completable future that
2:05
S…
Speaker 2 (16- Handling timeouts)
will complete after one second.
2:07
S…
Speaker 1 (16- Handling timeouts)
So let me reformat the code.
2:10
S…
Speaker 1 (16- Handling timeouts)
That is better.
2:12
S…
Speaker 2 (16- Handling timeouts)
Now let's run the program again.
2:15
S…
Speaker 1 (16- Handling timeouts)
This time,
2:15
S…
Speaker 2 (16- Handling timeouts)
after one second,
2:16
S…
Speaker 2 (16- Handling timeouts)
we got our default value.
2:18
S…
Speaker 1 (16- Handling timeouts)
This is a better experience for the end user.
Эта запись была составлена АИ (автоматическое распознавание речи). Политика МА
Резюме
Нажмите Нажмите Обобщение для составления резюме этой стенограммы.
Резюмируя...
Спросите AI об этом писце
Спросите что - нибудь об этой стенограмме — МА найдет соответствующие разделы и ответ.