Viser kun
0:03
S… Speaker 1 (18- Concurrent Collections)
Synchronized collections achieve thread safety by using locks.
0:07
S… Speaker 1 (18- Concurrent Collections)
So when a thread gets access to a synchronous collection,
0:10
S… Speaker 1 (18- Concurrent Collections)
the entire collection gets locked and other threads have to wait.
0:13
S… Speaker 1 (18- Concurrent Collections)
This works well for most cases,
0:15
S… Speaker 1 (18- Concurrent Collections)
but it can have a negative impact on the performance and scalability as
0:19
S… Speaker 1 (18- Concurrent Collections)
the number of threads and concurrent operations increase.
0:22
S… Speaker 1 (18- Concurrent Collections)
In those situations we can use concurrent collections in Java.
0:26
S… Speaker 1 (18- Concurrent Collections)
These classes use the partitioning technique to allow concurrency.
0:30
S… Speaker 1 (18- Concurrent Collections)
So they divide their data into segments and different threads can concurrently
0:34
S… Speaker 1 (18- Concurrent Collections)
work with different segments,
0:36
S… Speaker 1 (18- Concurrent Collections)
but only one thread at a time can access a given segment.
0:39
S… Speaker 1 (18- Concurrent Collections)
So concurrent collections are faster than synchronized collections because they
0:43
S… Speaker 1 (18- Concurrent Collections)
don't use synchronization.
0:45
S… Speaker 1 (18- Concurrent Collections)
These classes are declared in the java .util .concurrent package.
0:49
S… Speaker 1 (18- Concurrent Collections)
For example,
0:50
S… Speaker 1 (18- Concurrent Collections)
we have concurrent hash map,
0:51
S… Speaker 1 (18- Concurrent Collections)
we have concurrent link deck,
0:53
S… Speaker 1 (18- Concurrent Collections)
which is a double ended queue implemented using a linked list.
0:57
S… Speaker 1 (18- Concurrent Collections)
We have concurrent map and so on.
0:59
S… Speaker 1 (18- Concurrent Collections)
So let's take a look at an example.
1:01
S… Speaker 1 (18- Concurrent Collections)
If you want to create a regular hash map,
1:04
S… Speaker 1 (18- Concurrent Collections)
we write map of integer and
1:08
S… Speaker 1 (18- Concurrent Collections)
string.
1:09
S… Speaker 1 (18- Concurrent Collections)
We call it map and set it to a new hash map.
1:13
S… Speaker 1 (18- Concurrent Collections)
This is a regular hash map where our keys are integers and our values
1:18
S… Speaker 1 (18- Concurrent Collections)
are strings.
1:19
S… Speaker 1 (18- Concurrent Collections)
Here we can call map .put to add an entry
1:23
S… Speaker 1 (18- Concurrent Collections)
like 1 and a.
1:24
S… Speaker 1 (18- Concurrent Collections)
We can call map .get to get the value
1:28
S… Speaker 1 (18- Concurrent Collections)
associated with the key.
1:29
S… Speaker 1 (18- Concurrent Collections)
We can call map .remove and so on.
1:32
S… Speaker 1 (18- Concurrent Collections)
Now this is a regular hash map implementation that
1:36
S… Speaker 1 (18- Concurrent Collections)
is not thread safe.
1:37
S… Speaker 1 (18- Concurrent Collections)
So if it's going to get access only by a single thread,
1:40
S… Speaker 1 (18- Concurrent Collections)
it's perfectly fine.
1:41
S… Speaker 1 (18- Concurrent Collections)
But if you want to share this across many different threads,
1:44
S… Speaker 1 (18- Concurrent Collections)
we're going to have problems.
1:45
S… Speaker 1 (18- Concurrent Collections)
Now to solve this,
1:47
S… Speaker 1 (18- Concurrent Collections)
all we have to do is to replace the hash map with the
1:51
S… Speaker 1 (18- Concurrent Collections)
concurrent hash map.
1:52
S… Speaker 1 (18- Concurrent Collections)
That's all we had to do.
1:53
S… Speaker 1 (18- Concurrent Collections)
Everything else in our code stays the same.
1:56
S… Speaker 1 (18- Concurrent Collections)
Because this map is an interface,
1:59
S… Speaker 1 (18- Concurrent Collections)
and hash map and concurrent hash map are two
2:03
S… Speaker 1 (18- Concurrent Collections)
different implementations of this interface.
2:06
S… Speaker 1 (18- Concurrent Collections)
So by programming against interfaces,
2:08
S… Speaker 1 (18- Concurrent Collections)
we reduce the impact of changes in our applications.
2:11
S… Speaker 1 (18- Concurrent Collections)
We simply substitute one implementation with another.
2:15
S… Speaker 1 (18- Concurrent Collections)
Everything else stays the same.
2:18
S… Speaker 2 (18- Concurrent Collections)
Again,
2:19
S… Speaker 1 (18- Concurrent Collections)
I cannot emphasize the importance of interfaces enough.
2:22
S… Speaker 1 (18- Concurrent Collections)
A lot of developers don't understand interfaces.
2:24
S… Speaker 1 (18- Concurrent Collections)
They think they do,
2:25
S… Speaker 1 (18- Concurrent Collections)
but honestly,
2:26
S… Speaker 1 (18- Concurrent Collections)
they don't.
2:27
S… Speaker 1 (18- Concurrent Collections)
So if you want to have a better understanding of interfaces and their power,
2:30
S… Speaker 1 (18- Concurrent Collections)
watch the second part of this series.
2:32
S… Speaker 1 (18- Concurrent Collections)
It will change how you think about building software.

Denne udskrift blev genereret af AI (automatisk talegenkendelse). Kan indeholde fejl! verificere mod den oprindelige lyd til kritisk brug. AI-politik

❤️ Elsker du STT.ai? Fortæl det til dine venner!
Oversigt
Klik på Summarize for at generere en AI resumé af denne udskrift.
Opsummering...
Spørg AI om denne transskription
Spørg om noget om denne udskrift! AI vil finde relevante sektioner og svar.