14- Waiting for Many Tasks to Complete
Jul 04, 2026 01:18
· 2:48
· English
· Whisper Turbo
· 2 Đầu ghi
Bản ghi chép này hết hạn hôm nay.
Tăng cấp cho lưu trữ vĩnh viễn →
Chỉ hiển thị
0:03
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Sometimes we need to wait for the completion of many tasks before doing something else.
0:07
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Let me show you how to do this.
0:09
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
So I'm going to create three completable features to represent three
0:13
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
asynchronous tasks.
0:14
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Here's the first one.
0:16
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
We set this to completable future that supply sync
0:20
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
and here we return one.
0:22
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
Here's our second task.
0:24
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
We return two and here's the third task.
0:30
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Now, we want to wait for all these tasks to complete before doing something else.
0:34
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
To do that we call completable future dot all
0:39
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
of.
0:39
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
Now look at the signature of this method.
0:41
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Here we have completable future dot dot dot.
0:44
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
This is called var args which means a variable number of arguments.
0:49
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
We can pass zero or more arguments.
0:50
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
So I'm going to pass first,
0:52
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
second,
0:53
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
and third.
0:55
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Now this will return a new completable future that will complete when
0:59
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
all of these completable futures complete.
1:01
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
So let's store the result over here.
1:04
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Look at the type of this variable that is a completable future
1:09
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
of void.
1:10
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Why is it void?
1:11
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Because each completable future may return a different data type,
1:14
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
one might return a string,
1:16
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
another completable future might return an integer.
1:19
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
So there is no way for us to know what is the final result.
1:22
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
That is why this is a completable future of void.
1:25
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
It's like a task that will complete when many other tasks complete.
1:29
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
Okay?
1:30
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
So,
1:31
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
here we can write all dot then run,
1:35
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
here we pass a runnable object,
1:38
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
and then print
1:42
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
all tasks completed successfully.
1:45
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
Let's run our program.
1:48
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
So here's the message.
1:52
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
But what if you want to get the result of each of these completable future
1:56
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
objects?
1:56
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
Well, that is very easy.
1:59
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
We can call first that get this will return the
2:03
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
result of the first completable future.
2:05
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
But in this case,
2:06
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
this method is not going to block the current thread because this code
2:11
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
this task is going to get executed asynchronously when
2:16
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
all these tasks are complete.
2:17
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
So at this point,
2:19
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
when we call the get method on any of these completable features,
2:22
S…
Speaker 2 (14- Waiting for Many Tasks to Complete)
we get the result immediately because the result is ready.
2:26
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
So we get the result of the first task and store
2:30
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
it here.
2:31
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
First result.
2:33
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
Now we should wrap this inside a try catch block.
2:36
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
So we get the first result and then print it.
2:43
S…
Speaker 1 (14- Waiting for Many Tasks to Complete)
So here's the first result and our final message.
Bản ghi này được tạo bởi AI (tự động nhận dạng giọng nói). Có thể chứa lỗi — kiểm tra với âm thanh gốc để sử dụng quan trọng. Chính sách AI
Tóm tắt
Nhấn tổng hợp để tạo một tóm tắt AI của bản ghi này.
Tóm tắt...
Hỏi AI về bản ghi này
Hỏi bất cứ điều gì về bản ghi chép này - AI sẽ tìm thấy các phần liên quan và trả lời.