8- Race Conditions
Jul 03, 2026 14:45
· 5:53
· English
· Whisper Turbo
· 2 ஒலிப்பேழைகள்
இந்த பதிவு இன்று காலாவதியாகிறது.
நிலையான சேமிப்புக்காக மேம்படுத்தல் →
காட்டு
0:03
S…
Speaker 1 (8- Race Conditions)
Let's say as part of downloading multiple files,
0:05
S…
Speaker 1 (8- Race Conditions)
we want to show the total number of bytes we have downloaded so far.
0:09
S…
Speaker 1 (8- Race Conditions)
So we need to store the total value somewhere and have multiple threads
0:13
S…
Speaker 1 (8- Race Conditions)
incremented as they are downloading files.
0:15
S…
Speaker 1 (8- Race Conditions)
This is going to cause a race condition which means multiple threads
0:19
S…
Speaker 1 (8- Race Conditions)
racing or competing to modify a shared resource.
0:22
S…
Speaker 1 (8- Race Conditions)
Let me show you.
0:24
S…
Speaker 1 (8- Race Conditions)
So I'm going to add a new class in this project.
0:26
S…
Speaker 1 (8- Race Conditions)
We call it download status.
0:31
S…
Speaker 1 (8- Race Conditions)
Here we need a field for storing the total number of bytes we have downloaded.
0:35
S…
Speaker 1 (8- Race Conditions)
So private integer total bytes Now
0:39
S…
Speaker 1 (8- Race Conditions)
let's create a getter for this field.
0:40
S…
Speaker 1 (8- Race Conditions)
So we put the caret on the field name press alt and enter and Create
0:45
S…
Speaker 1 (8- Race Conditions)
a getter.
0:46
S…
Speaker 1 (8- Race Conditions)
We also need a method for incrementing this field.
0:48
S…
Speaker 1 (8- Race Conditions)
So instead of adding a setter I would prefer to add an increment method
0:53
S…
Speaker 1 (8- Race Conditions)
So each thread can call this to increment our field.
0:56
S…
Speaker 1 (8- Race Conditions)
It prevents us from accidentally resetting the total bytes in a thread
1:00
S…
Speaker 1 (8- Race Conditions)
So, public void increment total bytes,
1:04
S…
Speaker 1 (8- Race Conditions)
and here we type total bytes plus plus.
1:08
S…
Speaker 1 (8- Race Conditions)
Now we want all our download threads to report to a single download
1:12
S…
Speaker 1 (8- Race Conditions)
status object.
1:13
S…
Speaker 1 (8- Race Conditions)
So,
1:14
S…
Speaker 1 (8- Race Conditions)
in our demo class,
1:15
S…
Speaker 1 (8- Race Conditions)
let's create a download status object,
1:19
S…
Speaker 1 (8- Race Conditions)
new download status.
1:21
S…
Speaker 1 (8- Race Conditions)
Now we start 10 download threads.
1:24
S…
Speaker 1 (8- Race Conditions)
So,
1:24
S…
Speaker 1 (8- Race Conditions)
let's add a for loop for i equals 0,
1:27
S…
Speaker 1 (8- Race Conditions)
10 times plus plus.
1:31
S…
Speaker 1 (8- Race Conditions)
Here we create a thread,
1:32
S…
Speaker 1 (8- Race Conditions)
new thread.
1:33
S…
Speaker 1 (8- Race Conditions)
Here we pass a new download file task.
1:37
S…
Speaker 1 (8- Race Conditions)
We start the thread.
1:39
S…
Speaker 2 (8- Race Conditions)
Now,
1:41
S…
Speaker 1 (8- Race Conditions)
we should pass this status object to each download file task.
1:44
S…
Speaker 2 (8- Race Conditions)
So,
1:45
S…
Speaker 1 (8- Race Conditions)
let's pass it here.
1:46
S…
Speaker 2 (8- Race Conditions)
Now,
1:48
S…
Speaker 1 (8- Race Conditions)
we let IntelliJ create a constructor for receiving this value.
1:52
S…
Speaker 2 (8- Race Conditions)
So,
1:53
S…
Speaker 1 (8- Race Conditions)
we press alt and enter.
1:53
S…
Speaker 1 (8- Race Conditions)
Create constructor.
1:55
S…
Speaker 1 (8- Race Conditions)
There you go.
1:57
S…
Speaker 1 (8- Race Conditions)
Pretty easy.
1:58
S…
Speaker 1 (8- Race Conditions)
Now,
1:59
S…
Speaker 1 (8- Race Conditions)
we should create a fill to store this status object.
2:01
S…
Speaker 1 (8- Race Conditions)
So, we press alt and enter here one more time.
2:04
S…
Speaker 1 (8- Race Conditions)
and create a field for parameter status.
2:07
S…
Speaker 1 (8- Race Conditions)
There you go.
2:10
S…
Speaker 1 (8- Race Conditions)
So here's our field and we're setting it in the constructor.
2:12
S…
Speaker 1 (8- Race Conditions)
Now let's change the run method a little bit.
2:15
S…
Speaker 1 (8- Race Conditions)
Instead of integer dot max value,
2:18
S…
Speaker 1 (8- Race Conditions)
let's use 10 ,000.
2:21
S…
Speaker 1 (8- Race Conditions)
So we want to simulate a scenario where each file is 10 ,000
2:25
S…
Speaker 1 (8- Race Conditions)
bytes.
2:26
S…
Speaker 1 (8- Race Conditions)
We can also add an underline here.
2:28
S…
Speaker 1 (8- Race Conditions)
This makes our code cleaner and more readable.
2:30
S…
Speaker 1 (8- Race Conditions)
So in each iteration,
2:33
S…
Speaker 1 (8- Race Conditions)
we call status dot increment total bytes.
2:37
S…
Speaker 1 (8- Race Conditions)
Now, we don't need this message anymore,
2:39
S…
Speaker 1 (8- Race Conditions)
so let's simplify our code.
2:41
S…
Speaker 1 (8- Race Conditions)
Now back to our demo class.
2:43
S…
Speaker 1 (8- Race Conditions)
We are starting 10 download threads and sharing a single
2:47
S…
Speaker 1 (8- Race Conditions)
status object across these threads.
2:50
S…
Speaker 2 (8- Race Conditions)
Now,
2:51
S…
Speaker 1 (8- Race Conditions)
once all these threads are complete,
2:53
S…
Speaker 1 (8- Race Conditions)
we should print the total number of bytes we have downloaded.
2:56
S…
Speaker 1 (8- Race Conditions)
So we have to wait for all these threads to finish.
2:58
S…
Speaker 2 (8- Race Conditions)
Now,
2:59
S…
Speaker 1 (8- Race Conditions)
we cannot call thread the join
3:03
S…
Speaker 1 (8- Race Conditions)
because this will make the main thread wait for each download to finish before starting
3:07
S…
Speaker 1 (8- Race Conditions)
another download.
3:08
S…
Speaker 1 (8- Race Conditions)
Because this join method is a blocking method.
3:10
S…
Speaker 1 (8- Race Conditions)
So in the first iteration,
3:12
S…
Speaker 1 (8- Race Conditions)
we create a thread,
3:13
S…
Speaker 1 (8- Race Conditions)
we start it,
3:14
S…
Speaker 1 (8- Race Conditions)
and then we wait for that thread to finish before going to the second iteration to create a
3:18
S…
Speaker 1 (8- Race Conditions)
second thread.
3:18
S…
Speaker 1 (8- Race Conditions)
So we can use the join method here.
3:21
S…
Speaker 1 (8- Race Conditions)
We should start all these threads simultaneously and then join
3:25
S…
Speaker 1 (8- Race Conditions)
with all of them.
3:26
S…
Speaker 1 (8- Race Conditions)
So let's declare a list of threads,
3:29
S…
Speaker 1 (8- Race Conditions)
a list of thread,
3:33
S…
Speaker 1 (8- Race Conditions)
we call it threads and set it to a new array list.
3:36
S…
Speaker 2 (8- Race Conditions)
Now,
3:39
S…
Speaker 1 (8- Race Conditions)
every time we start a thread,
3:40
S…
Speaker 1 (8- Race Conditions)
we add it to our list.
3:42
S…
Speaker 1 (8- Race Conditions)
So threads .add thread.
3:44
S…
Speaker 1 (8- Race Conditions)
Now here we need another for loop to
3:49
S…
Speaker 1 (8- Race Conditions)
iterate over all these threads and join with them.
3:51
S…
Speaker 1 (8- Race Conditions)
So, for thread in threads,
3:55
S…
Speaker 1 (8- Race Conditions)
we simply call thread .join.
3:59
S…
Speaker 1 (8- Race Conditions)
Now once again,
4:00
S…
Speaker 1 (8- Race Conditions)
we should handle the interrupted exception,
4:02
S…
Speaker 1 (8- Race Conditions)
so let's do it real quick.
4:06
S…
Speaker 1 (8- Race Conditions)
So with this for loop,
4:08
S…
Speaker 1 (8- Race Conditions)
we can wait for all these download threads to finish.
4:10
S…
Speaker 1 (8- Race Conditions)
Then we can print the total number of bytes we have downloaded.
4:15
S…
Speaker 1 (8- Race Conditions)
So we call status dot get total bytes.
4:19
S…
Speaker 1 (8- Race Conditions)
So here we have 10 download tasks and
4:23
S…
Speaker 1 (8- Race Conditions)
each task is going to download 10 ,000 bytes.
4:25
S…
Speaker 1 (8- Race Conditions)
So when I run this program,
4:27
S…
Speaker 1 (8- Race Conditions)
we expect to see 100 ,000 bytes,
4:29
S…
Speaker 1 (8- Race Conditions)
but that's not going to happen.
4:31
S…
Speaker 1 (8- Race Conditions)
Let me show you.
4:32
S…
Speaker 1 (8- Race Conditions)
So run.
4:34
S…
Speaker 1 (8- Race Conditions)
So we saw 78 ,000.
4:36
S…
Speaker 1 (8- Race Conditions)
If I run it again,
4:38
S…
Speaker 1 (8- Race Conditions)
we see a different number,
4:40
S…
Speaker 1 (8- Race Conditions)
72 ,000.
4:40
S…
Speaker 1 (8- Race Conditions)
One more time,
4:41
S…
Speaker 1 (8- Race Conditions)
71 ,000.
4:43
S…
Speaker 1 (8- Race Conditions)
This is a race condition in action because multiple threads are
4:47
S…
Speaker 1 (8- Race Conditions)
racing or competing to update the total number of bytes.
4:50
S…
Speaker 1 (8- Race Conditions)
Now let me explain what happens under the hood.
4:53
S…
Speaker 1 (8- Race Conditions)
So back to our download status class,
4:56
S…
Speaker 1 (8- Race Conditions)
look at this increment operator here.
4:59
S…
Speaker 1 (8- Race Conditions)
This operation involves three steps.
5:01
S…
Speaker 1 (8- Race Conditions)
So even though we have only one line of code,
5:03
S…
Speaker 1 (8- Race Conditions)
there will be three steps happening under the hood.
5:06
S…
Speaker 1 (8- Race Conditions)
First,
5:06
S…
Speaker 1 (8- Race Conditions)
the value of this field has to be read from the main memory and stored
5:11
S…
Speaker 1 (8- Race Conditions)
in the CPU.
5:11
S…
Speaker 1 (8- Race Conditions)
Next,
5:13
S…
Speaker 1 (8- Race Conditions)
the CPU is going to increment this value and then the updated value is going
5:17
S…
Speaker 1 (8- Race Conditions)
to be stored in the memory.
5:18
S…
Speaker 1 (8- Race Conditions)
So we have three steps and we call this a non -atomic
5:22
S…
Speaker 1 (8- Race Conditions)
operation because it involves multiple steps.
5:25
S…
Speaker 1 (8- Race Conditions)
In contrast,
5:26
S…
Speaker 1 (8- Race Conditions)
an atomic operation is like an atom.
5:28
S…
Speaker 1 (8- Race Conditions)
We cannot break it down into many steps.
5:30
S…
Speaker 2 (8- Race Conditions)
Now,
5:31
S…
Speaker 1 (8- Race Conditions)
imagine two threads trying to call into this method at the same time.
5:35
S…
Speaker 1 (8- Race Conditions)
Let's say the value of this field is zero.
5:37
S…
Speaker 1 (8- Race Conditions)
Both these threads will read this value concurrently.
5:40
S…
Speaker 1 (8- Race Conditions)
They both increment it and write it to the memory.
5:42
S…
Speaker 1 (8- Race Conditions)
So the result would be one instead of two.
5:45
S…
Speaker 1 (8- Race Conditions)
This is how we lose an update.
5:47
S…
Speaker 1 (8- Race Conditions)
In the next video,
5:48
S…
Speaker 1 (8- Race Conditions)
we'll be looking at various strategies to prevent these problems.
இந்த ஒலிப்பதிவு AI (தானியங்கி பேச்சு அடையாளம்) மூலம் உருவாக்கப்பட்டது. பிழைகள் இருக்கலாம் - முக்கியமான பயன்பாட்டுக்கு மூல ஒலியை சரிபார்க்கவும். AI கொள்கை
சுருக்கம்
இந்த நகலெடுப்புக்கான AI சுருக்கத்தை உருவாக்க சுருக்கம் என்பதை க்ளிக் செய்யவும்.
சுருக்கப்படுகிறது...
இந்த நகலை AI க்குக் கேட்கவும்
இந்த நூலைப் பற்றி ஏதாவது கேட்கவும் - AI தொடர்புடைய பகுதிகளைக் கண்டுபிடித்து பதிலளிக்கும்.