12- The synchronized Keyword
Jul 03, 2026 22:30
· 5:13
· English
· Whisper Turbo
· 2 Konekte
Transkript sa a ekspire jodi a.
Ajoutè pou depo perpétuel →
Montre sèlman
0:03
S…
Speaker 1 (12- The synchronized Keyword)
So you learn how to use locks to implement synchronization.
0:06
S…
Speaker 1 (12- The synchronized Keyword)
Now in Java we have another tool for implementing synchronization and
0:10
S…
Speaker 1 (12- The synchronized Keyword)
that's the synchronized keyword.
0:12
S…
Speaker 1 (12- The synchronized Keyword)
Using the synchronized keyword we can achieve the same result but without explicitly
0:16
S…
Speaker 1 (12- The synchronized Keyword)
creating a lock object and locking and unlocking it.
0:20
S…
Speaker 1 (12- The synchronized Keyword)
So our code looks simpler.
0:21
S…
Speaker 1 (12- The synchronized Keyword)
Let me show you.
0:22
S…
Speaker 1 (12- The synchronized Keyword)
First I'm going to delete the code that we wrote in the last video.
0:25
S…
Speaker 1 (12- The synchronized Keyword)
So delete and delete.
0:28
S…
Speaker 1 (12- The synchronized Keyword)
We also don't need this lock object
0:32
S…
Speaker 1 (12- The synchronized Keyword)
anymore.
0:33
S…
Speaker 1 (12- The synchronized Keyword)
So if we run our program,
0:34
S…
Speaker 1 (12- The synchronized Keyword)
now we should have a race condition.
0:36
S…
Speaker 1 (12- The synchronized Keyword)
There you go.
0:37
S…
Speaker 1 (12- The synchronized Keyword)
So to prevent this,
0:39
S…
Speaker 1 (12- The synchronized Keyword)
we want to make sure that only one thread at a time can execute this piece of
0:43
S…
Speaker 1 (12- The synchronized Keyword)
code. To do that,
0:44
S…
Speaker 1 (12- The synchronized Keyword)
we wrap this inside a synchronized block
0:49
S…
Speaker 2 (12- The synchronized Keyword)
like this.
0:51
S…
Speaker 1 (12- The synchronized Keyword)
Now here in parenthesis,
0:53
S…
Speaker 1 (12- The synchronized Keyword)
we should pass an object.
0:54
S…
Speaker 1 (12- The synchronized Keyword)
This is what we call a monitor object.
0:57
S…
Speaker 1 (12- The synchronized Keyword)
Every object in Java has a built -in lock.
1:00
S…
Speaker 1 (12- The synchronized Keyword)
So Java is going to get the built -in lock from that object and use it under
1:04
S…
Speaker 1 (12- The synchronized Keyword)
the hood.
1:04
S…
Speaker 1 (12- The synchronized Keyword)
For example,
1:05
S…
Speaker 1 (12- The synchronized Keyword)
we can pass this as a reference to the current object,
1:08
S…
Speaker 1 (12- The synchronized Keyword)
but this is a bad practice.
1:10
S…
Speaker 1 (12- The synchronized Keyword)
I will explain why in a second.
1:11
S…
Speaker 1 (12- The synchronized Keyword)
First,
1:12
S…
Speaker 1 (12- The synchronized Keyword)
let's run our program again and see if the synchronized keyword actually solved
1:16
S…
Speaker 1 (12- The synchronized Keyword)
our problem or not.
1:17
S…
Speaker 1 (12- The synchronized Keyword)
So,
1:18
S…
Speaker 1 (12- The synchronized Keyword)
run.
1:18
S…
Speaker 1 (12- The synchronized Keyword)
There you go.
1:21
S…
Speaker 1 (12- The synchronized Keyword)
The problem is fixed.
1:22
S…
Speaker 1 (12- The synchronized Keyword)
But why is passing this a bad practice here?
1:25
S…
Speaker 1 (12- The synchronized Keyword)
well let's add another field in this class private
1:30
S…
Speaker 1 (12- The synchronized Keyword)
integer total files with this we can keep track of how many
1:34
S…
Speaker 1 (12- The synchronized Keyword)
files we have successfully downloaded so far now we need a getter
1:38
S…
Speaker 1 (12- The synchronized Keyword)
for this field so let's create it real quick create getter
1:42
S…
Speaker 1 (12- The synchronized Keyword)
for total files you also need an increment
1:46
S…
Speaker 1 (12- The synchronized Keyword)
method I'm going to type that manually so
1:50
S…
Speaker 1 (12- The synchronized Keyword)
public void,
1:51
S…
Speaker 1 (12- The synchronized Keyword)
increment total bytes,
1:53
S…
Speaker 1 (12- The synchronized Keyword)
sorry, total files,
1:54
S…
Speaker 1 (12- The synchronized Keyword)
and here we increment total files.
1:57
S…
Speaker 2 (12- The synchronized Keyword)
Now,
1:59
S…
Speaker 1 (12- The synchronized Keyword)
we want to make this new method thread safe as well,
2:01
S…
Speaker 1 (12- The synchronized Keyword)
because it is possible that two download threads finish concurrently,
2:05
S…
Speaker 1 (12- The synchronized Keyword)
so we don't want them to update the total files field at the same time.
2:09
S…
Speaker 1 (12- The synchronized Keyword)
So,
2:09
S…
Speaker 1 (12- The synchronized Keyword)
we wrap this inside a synchronized block with this as the
2:14
S…
Speaker 1 (12- The synchronized Keyword)
monitor object,
2:14
S…
Speaker 2 (12- The synchronized Keyword)
like this.
2:17
S…
Speaker 1 (12- The synchronized Keyword)
Now, with this implementation,
2:19
S…
Speaker 1 (12- The synchronized Keyword)
if one thread is calling the increment total bytes method,
2:23
S…
Speaker 1 (12- The synchronized Keyword)
another thread cannot call the increment total bytes method
2:27
S…
Speaker 1 (12- The synchronized Keyword)
because both these methods are using the same monitor objects.
2:31
S…
Speaker 1 (12- The synchronized Keyword)
So only one thread at a time can call into a synchronous method
2:35
S…
Speaker 1 (12- The synchronized Keyword)
of this object.
2:36
S…
Speaker 1 (12- The synchronized Keyword)
This is like having a single hotel room.
2:38
S…
Speaker 1 (12- The synchronized Keyword)
Each monitor object represents a hotel room.
2:41
S…
Speaker 1 (12- The synchronized Keyword)
So with this implementation,
2:42
S…
Speaker 1 (12- The synchronized Keyword)
if one thread is updating the total bytes,
2:45
S…
Speaker 1 (12- The synchronized Keyword)
another thread cannot update the total files field.
2:48
S…
Speaker 1 (12- The synchronized Keyword)
That's going to cause a lot of overhead.
2:50
S…
Speaker 1 (12- The synchronized Keyword)
The whole point of synchronization is to make sure that two threads cannot
2:54
S…
Speaker 1 (12- The synchronized Keyword)
modify the same data at the same time.
2:56
S…
Speaker 1 (12- The synchronized Keyword)
But here we're dealing with two different fields.
2:59
S…
Speaker 1 (12- The synchronized Keyword)
So using the same monitor object reduces the throughput of this
3:03
S…
Speaker 1 (12- The synchronized Keyword)
class.
3:03
S…
Speaker 1 (12- The synchronized Keyword)
It can cause unnecessary weights.
3:06
S…
Speaker 1 (12- The synchronized Keyword)
Now, in a simple application with a few threads,
3:08
S…
Speaker 1 (12- The synchronized Keyword)
you may be able to get away with it,
3:10
S…
Speaker 1 (12- The synchronized Keyword)
but in a larger application with more concurrency requirements,
3:13
S…
Speaker 1 (12- The synchronized Keyword)
you should use dedicated monitor objects.
3:16
S…
Speaker 1 (12- The synchronized Keyword)
Let me show you.
3:16
S…
Speaker 1 (12- The synchronized Keyword)
So,
3:17
S…
Speaker 1 (12- The synchronized Keyword)
we add two new fields to this class.
3:21
S…
Speaker 1 (12- The synchronized Keyword)
Private object total bytes
3:25
S…
Speaker 1 (12- The synchronized Keyword)
lock.
3:26
S…
Speaker 1 (12- The synchronized Keyword)
We set it to a new object.
3:28
S…
Speaker 1 (12- The synchronized Keyword)
And one more time,
3:29
S…
Speaker 1 (12- The synchronized Keyword)
we create another field called total files lock.
3:34
S…
Speaker 1 (12- The synchronized Keyword)
Note that these are just plain object instances.
3:37
S…
Speaker 1 (12- The synchronized Keyword)
They're not lock objects,
3:38
S…
Speaker 1 (12- The synchronized Keyword)
okay? Now,
3:40
S…
Speaker 1 (12- The synchronized Keyword)
by object,
3:40
S…
Speaker 1 (12- The synchronized Keyword)
it's just a common convention.
3:42
S…
Speaker 1 (12- The synchronized Keyword)
Technically,
3:43
S…
Speaker 1 (12- The synchronized Keyword)
we could use any type here,
3:44
S…
Speaker 1 (12- The synchronized Keyword)
but it's more conventional to use the object class because we're not looking for any specific
3:48
S…
Speaker 1 (12- The synchronized Keyword)
behaviors,
3:49
S…
Speaker 1 (12- The synchronized Keyword)
okay?
3:50
S…
Speaker 1 (12- The synchronized Keyword)
Now,
3:51
S…
Speaker 1 (12- The synchronized Keyword)
in this method,
3:53
S…
Speaker 1 (12- The synchronized Keyword)
instead of synchronizing on this,
3:56
S…
Speaker 1 (12- The synchronized Keyword)
we're going to synchronize on total bytes lock
4:00
S…
Speaker 1 (12- The synchronized Keyword)
and
4:02
S…
Speaker 1 (12- The synchronized Keyword)
In this other method,
4:03
S…
Speaker 1 (12- The synchronized Keyword)
we're going to synchronize on total files lock.
4:06
S…
Speaker 1 (12- The synchronized Keyword)
So two different monitor objects.
4:09
S…
Speaker 1 (12- The synchronized Keyword)
Now there's another way to use the synchronize keyword.
4:12
S…
Speaker 1 (12- The synchronized Keyword)
Instead of using it as a block,
4:14
S…
Speaker 1 (12- The synchronized Keyword)
we can declare this entire method as synchronized.
4:17
S…
Speaker 1 (12- The synchronized Keyword)
So we type public synchronized void,
4:20
S…
Speaker 1 (12- The synchronized Keyword)
and this is exactly equivalent to writing synchronized this.
4:24
S…
Speaker 1 (12- The synchronized Keyword)
So this will wrap this entire method inside the synchronized
4:28
S…
Speaker 1 (12- The synchronized Keyword)
block.
4:29
S…
Speaker 1 (12- The synchronized Keyword)
Again, you should be cautious about this approach because this is going to synchronize on the
4:33
S…
Speaker 1 (12- The synchronized Keyword)
current object.
4:34
S…
Speaker 1 (12- The synchronized Keyword)
And as I told you earlier,
4:35
S…
Speaker 1 (12- The synchronized Keyword)
this can cause a lot of unnecessary weights.
4:37
S…
Speaker 1 (12- The synchronized Keyword)
So it's better to use the synchronized block with a dedicated monitor
4:42
S…
Speaker 1 (12- The synchronized Keyword)
object.
4:42
S…
Speaker 2 (12- The synchronized Keyword)
Now,
4:43
S…
Speaker 1 (12- The synchronized Keyword)
all that aside,
4:44
S…
Speaker 1 (12- The synchronized Keyword)
synchronization is bad.
4:45
S…
Speaker 1 (12- The synchronized Keyword)
Your code runs sequentially and you lose concurrency.
4:48
S…
Speaker 1 (12- The synchronized Keyword)
Also,
4:49
S…
Speaker 1 (12- The synchronized Keyword)
you may cause deadlocks and all sorts of crazy bugs that are hard to reproduce and
4:53
S…
Speaker 1 (12- The synchronized Keyword)
fix.
4:53
S…
Speaker 1 (12- The synchronized Keyword)
So as a best practice,
4:55
S…
Speaker 1 (12- The synchronized Keyword)
don't use it in new code.
4:56
S…
Speaker 1 (12- The synchronized Keyword)
I just included in this course in case you're maintaining an older code base
5:00
S…
Speaker 1 (12- The synchronized Keyword)
that uses synchronization.
5:02
S…
Speaker 1 (12- The synchronized Keyword)
So you understand what it is and how it works.
5:04
S…
Speaker 2 (12- The synchronized Keyword)
Next,
5:05
S…
Speaker 1 (12- The synchronized Keyword)
we're going to look at another solution as a lightweight and simpler alternative to synchronization.
Transkript sa a te kreye pa AI (automatic speech recognition). Li ka gen erè — tcheke ak son orijinal la pou itilize kritik. Politik AI
Resume
Klike sou Remak pou kreye yon remak AI de transkript sa a.
Enstale...
Konbyen tan pou m' fè transkript la?
Mande nenpòt bagay sou transkript sa a - AI a pral jwenn seksyon ki enpòtan yo epi reponn.