11- Locks
Jul 03, 2026 15:09
· 3:17
· English
· Whisper Turbo
· 2 Speakers
This transcript expires today.
Upgrade for permanent storage →
Showing only
0:03
S…
Speaker 1 (11- Locks)
Another strategy for preventing race conditions and visibility problems
0:07
S…
Speaker 1 (11- Locks)
is to prevent multiple threads from accessing an object at the same time.
0:11
S…
Speaker 1 (11- Locks)
This is called synchronization.
0:13
S…
Speaker 1 (11- Locks)
So we have to synchronize or coordinate multiple threads trying to
0:17
S…
Speaker 1 (11- Locks)
access an object.
0:18
S…
Speaker 1 (11- Locks)
We do that using locks.
0:20
S…
Speaker 1 (11- Locks)
So we put a lock on a block of our code and the Java virtual machine will
0:24
S…
Speaker 1 (11- Locks)
ensure that only one thread at a time can execute that block of code,
0:27
S…
Speaker 1 (11- Locks)
which is called the critical section.
0:30
S…
Speaker 1 (11- Locks)
In reality,
0:31
S…
Speaker 1 (11- Locks)
our code is going to get executed sequentially,
0:33
S…
Speaker 1 (11- Locks)
so we lose concurrency.
0:34
S…
Speaker 1 (11- Locks)
As a metaphor,
0:35
S…
Speaker 1 (11- Locks)
think of a hotel room.
0:37
S…
Speaker 1 (11- Locks)
Only one guest at a time can be in that room.
0:39
S…
Speaker 1 (11- Locks)
When a guest goes in,
0:40
S…
Speaker 1 (11- Locks)
they lock the door so others cannot enter.
0:42
S…
Speaker 1 (11- Locks)
They have to wait until that guest checks out.
0:45
S…
Speaker 1 (11- Locks)
Using locks in code is pretty much the same.
0:47
S…
Speaker 1 (11- Locks)
We have the same concept in databases as well.
0:49
S…
Speaker 1 (11- Locks)
I talked about this in my Ultimate SQL course.
0:53
S…
Speaker 1 (11- Locks)
So, we have the same code as before.
0:55
S…
Speaker 1 (11- Locks)
We are sharing a single status object across many
1:00
S…
Speaker 1 (11- Locks)
download tasks.
1:00
S…
Speaker 1 (11- Locks)
If we run this program,
1:02
S…
Speaker 1 (11- Locks)
we have a race condition because every time we see
1:06
S…
Speaker 1 (11- Locks)
a different value.
1:07
S…
Speaker 2 (11- Locks)
So,
1:08
S…
Speaker 1 (11- Locks)
multiple threads are racing to update the total bytes field.
1:11
S…
Speaker 2 (11- Locks)
Now,
1:12
S…
Speaker 1 (11- Locks)
let's see how we can use a log to solve this problem.
1:15
S…
Speaker 2 (11- Locks)
So,
1:16
S…
Speaker 1 (11- Locks)
back to our download status class,
1:18
S…
Speaker 1 (11- Locks)
we want to ensure that only one thread at a time can increment this field.
1:22
S…
Speaker 1 (11- Locks)
So here we declare a private field of type lock.
1:27
S…
Speaker 1 (11- Locks)
This is an interface declared in the java .util .concurrent
1:31
S…
Speaker 1 (11- Locks)
package.
1:32
S…
Speaker 1 (11- Locks)
So lock,
1:33
S…
Speaker 1 (11- Locks)
we call it lock,
1:34
S…
Speaker 1 (11- Locks)
and set it to a new reentrant lock.
1:38
S…
Speaker 1 (11- Locks)
This is one of the implementations of the lock interface.
1:41
S…
Speaker 2 (11- Locks)
Now,
1:42
S…
Speaker 1 (11- Locks)
in our increment method,
1:44
S…
Speaker 1 (11- Locks)
before we increment this field,
1:46
S…
Speaker 1 (11- Locks)
we call lock .lock.
1:49
S…
Speaker 1 (11- Locks)
So we call the lock method to lock this lock object.
1:52
S…
Speaker 1 (11- Locks)
I know it sounds weird.
1:53
S…
Speaker 1 (11- Locks)
Then we increment the total bytes fill and finally we
1:57
S…
Speaker 1 (11- Locks)
call lock that unlock.
2:00
S…
Speaker 1 (11- Locks)
So when a thread enters this method,
2:03
S…
Speaker 1 (11- Locks)
it will lock this lock object.
2:05
S…
Speaker 1 (11- Locks)
It's like a guest going to a hotel room and locking the door.
2:08
S…
Speaker 1 (11- Locks)
Other guests or other threads have to wait for this guest to
2:12
S…
Speaker 1 (11- Locks)
come out.
2:13
S…
Speaker 1 (11- Locks)
This is how locking works.
2:15
S…
Speaker 1 (11- Locks)
Now as a best practice,
2:16
S…
Speaker 1 (11- Locks)
we should execute this line in a finally block because if an exception is
2:20
S…
Speaker 1 (11- Locks)
thrown, we don't want to keep this lock locked forever.
2:23
S…
Speaker 1 (11- Locks)
Otherwise,
2:23
S…
Speaker 1 (11- Locks)
no other threats can execute this code.
2:25
S…
Speaker 1 (11- Locks)
And this may cause a deadlock and our application will crash.
2:28
S…
Speaker 1 (11- Locks)
So let's add a try block here.
2:32
S…
Speaker 1 (11- Locks)
We execute the main logic inside the try block.
2:36
S…
Speaker 1 (11- Locks)
If this causes an exception,
2:37
S…
Speaker 1 (11- Locks)
but the finally block will ensure
2:41
S…
Speaker 1 (11- Locks)
that we always unlock this lock.
2:44
S…
Speaker 1 (11- Locks)
Now in this particular case,
2:46
S…
Speaker 1 (11- Locks)
we don't really need the try finally block because incrementing this field is not
2:50
S…
Speaker 1 (11- Locks)
going to throw an exception.
2:51
S…
Speaker 1 (11- Locks)
But in your applications,
2:52
S…
Speaker 1 (11- Locks)
you might have more complex logic and somewhere along this logic,
2:56
S…
Speaker 1 (11- Locks)
you might get an exception.
2:57
S…
Speaker 1 (11- Locks)
So make sure to unlock this lock inside of finally block.
3:00
S…
Speaker 2 (11- Locks)
Now,
3:01
S…
Speaker 1 (11- Locks)
let's run our program and see the result.
3:03
S…
Speaker 1 (11- Locks)
So we get 100 ,000.
3:06
S…
Speaker 1 (11- Locks)
Let's run it one more time.
3:07
S…
Speaker 2 (11- Locks)
There you go.
3:10
S…
Speaker 1 (11- Locks)
So, this is how we can use locks to prevent concurrency problems.
This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy
Summary
Click Summarize to generate an AI summary of this transcript.
Summarizing...
Ask AI About This Transcript
Ask anything about this transcript — the AI will find relevant sections and answer.