17- Synchronized Collections
Jul 03, 2026 22:42
· 3:24
· 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 1 (17- Synchronized Collections)
Sometimes we need to share a collection across many threads.
0:06
S…
Speaker 1 (17- Synchronized Collections)
So let's create a collection object collection of
0:10
S…
Speaker 1 (17- Synchronized Collections)
integers.
0:12
S…
Speaker 1 (17- Synchronized Collections)
We call it collection and set it to a new array
0:16
S…
Speaker 1 (17- Synchronized Collections)
list.
0:16
S…
Speaker 1 (17- Synchronized Collections)
Now let's create two threads and have each thread
0:20
S…
Speaker 1 (17- Synchronized Collections)
add three items to this collection.
0:22
S…
Speaker 1 (17- Synchronized Collections)
So thread one,
0:24
S…
Speaker 1 (17- Synchronized Collections)
let's set this to a new thread.
0:26
S…
Speaker 1 (17- Synchronized Collections)
Now here I'm going to use a lambda expression to represent a
0:30
S…
Speaker 1 (17- Synchronized Collections)
runnable object.
0:32
S…
Speaker 1 (17- Synchronized Collections)
Because this runnable interface has a single abstract method that
0:36
S…
Speaker 1 (17- Synchronized Collections)
is run, so it's a functional interface and we can represent it using a lambda expression.
0:40
S…
Speaker 1 (17- Synchronized Collections)
Now this method doesn't have any parameters and returns void.
0:44
S…
Speaker 1 (17- Synchronized Collections)
So over here,
0:46
S…
Speaker 1 (17- Synchronized Collections)
I'm going to pass empty parenthesis,
0:49
S…
Speaker 1 (17- Synchronized Collections)
an arrow,
0:50
S…
Speaker 1 (17- Synchronized Collections)
and a block.
0:51
S…
Speaker 1 (17- Synchronized Collections)
Now in the body of this lambda,
0:54
S…
Speaker 1 (17- Synchronized Collections)
we're going to call collection .addAll
0:57
S…
Speaker 1 (17- Synchronized Collections)
Here we want to pass three items.
0:59
S…
Speaker 1 (17- Synchronized Collections)
So let's use arrays that has list and pass one
1:03
S…
Speaker 1 (17- Synchronized Collections)
two and three Now let's duplicate this code
1:07
S…
Speaker 1 (17- Synchronized Collections)
So we create another thread and
1:12
S…
Speaker 1 (17- Synchronized Collections)
call this one thread two and with this we're going to add four
1:17
S…
Speaker 1 (17- Synchronized Collections)
five and six Next we start both
1:21
S…
Speaker 1 (17- Synchronized Collections)
these threads.
1:22
S…
Speaker 1 (17- Synchronized Collections)
So thread one does start and thread two does start then
1:27
S…
Speaker 1 (17- Synchronized Collections)
we join with them
1:29
S…
Speaker 1 (17- Synchronized Collections)
So thread one the join and thread to the join that
1:33
S…
Speaker 1 (17- Synchronized Collections)
we should wrap this inside the try catch block.
1:36
S…
Speaker 1 (17- Synchronized Collections)
There you go Let's move this line over here
1:40
S…
Speaker 1 (17- Synchronized Collections)
as well.
1:40
S…
Speaker 1 (17- Synchronized Collections)
So once they're finished,
1:44
S…
Speaker 1 (17- Synchronized Collections)
let's print what we have in the collection Take
1:48
S…
Speaker 2 (17- Synchronized Collections)
a look.
1:49
S…
Speaker 1 (17- Synchronized Collections)
So we have four five six one of
1:53
S…
Speaker 1 (17- Synchronized Collections)
our insertions was lost This is because we have a race condition two threads
1:57
S…
Speaker 1 (17- Synchronized Collections)
are racing or competing to modify our collection concurrently
2:03
S…
Speaker 1 (17- Synchronized Collections)
Now, to solve this problem,
2:04
S…
Speaker 1 (17- Synchronized Collections)
we can use a synchronized collection,
2:06
S…
Speaker 1 (17- Synchronized Collections)
which is a wrapper around a regular collection type in Java.
2:10
S…
Speaker 1 (17- Synchronized Collections)
So instead of setting this collection to a new array list,
2:13
S…
Speaker 1 (17- Synchronized Collections)
we're going to call collections.
2:16
S…
Speaker 1 (17- Synchronized Collections)
This class has a bunch of static factory methods for creating
2:20
S…
Speaker 1 (17- Synchronized Collections)
synchronized collections.
2:21
S…
Speaker 1 (17- Synchronized Collections)
So we have synchronized collection.
2:23
S…
Speaker 1 (17- Synchronized Collections)
Let me put this on a new line so we can see clearly.
2:26
S…
Speaker 2 (17- Synchronized Collections)
Take a look.
2:27
S…
Speaker 1 (17- Synchronized Collections)
We have synchronized collection,
2:29
S…
Speaker 1 (17- Synchronized Collections)
you have synchronized list,
2:30
S…
Speaker 1 (17- Synchronized Collections)
synchronized set,
2:31
S…
Speaker 1 (17- Synchronized Collections)
synchronized map,
2:32
S…
Speaker 1 (17- Synchronized Collections)
and so on.
2:32
S…
Speaker 1 (17- Synchronized Collections)
So let's call synchronized collection.
2:36
S…
Speaker 1 (17- Synchronized Collections)
Here we should pass an object that implements the collection
2:40
S…
Speaker 1 (17- Synchronized Collections)
interface, like an array list.
2:41
S…
Speaker 1 (17- Synchronized Collections)
So new array list.
2:45
S…
Speaker 1 (17- Synchronized Collections)
Internally, our objects are stored inside this array list,
2:49
S…
Speaker 1 (17- Synchronized Collections)
but when we call this method,
2:50
S…
Speaker 1 (17- Synchronized Collections)
this method is going to wrap this array list inside a synchronized collection.
2:54
S…
Speaker 1 (17- Synchronized Collections)
In that synchronized collection,
2:56
S…
Speaker 1 (17- Synchronized Collections)
all the methods like add,
2:58
S…
Speaker 1 (17- Synchronized Collections)
remove and so on,
2:59
S…
Speaker 1 (17- Synchronized Collections)
they have synchronization code.
3:01
S…
Speaker 1 (17- Synchronized Collections)
So synchronized collection essentially wraps a regular collection like
3:05
S…
Speaker 1 (17- Synchronized Collections)
an array list,
3:06
S…
Speaker 1 (17- Synchronized Collections)
but it makes it synchronized.
3:07
S…
Speaker 1 (17- Synchronized Collections)
Okay, now with this simple change,
3:09
S…
Speaker 1 (17- Synchronized Collections)
when we run our program,
3:11
S…
Speaker 1 (17- Synchronized Collections)
we can see that two threads
3:15
S…
Speaker 1 (17- Synchronized Collections)
modify our collection concurrently.
3:17
S…
Speaker 2 (17- Synchronized Collections)
So,
3:18
S…
Speaker 1 (17- Synchronized Collections)
this is how we can create a synchronous collection.
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.