13- The volatile Keyword
Jul 03, 2026 22:33
· 6:33
· English
· Whisper Turbo
· 2 اسپيڪر
هيءَ ترانسڪريٽ اڄ ختم ٿيندي.
ساري وقت جي ذخيري لاءِ اپ گريڊ →
صرف ڏيکارڻ
0:03
S…
Speaker 2 (13- The volatile Keyword)
In Java,
0:04
S…
Speaker 2 (13- The volatile Keyword)
we have another tool for writing thread -safe code,
0:06
S…
Speaker 2 (13- The volatile Keyword)
but without the overhead of synchronization.
0:09
S…
Speaker 2 (13- The volatile Keyword)
It's the volatile keyword.
0:10
S…
Speaker 2 (13- The volatile Keyword)
It solves the visibility problem,
0:12
S…
Speaker 2 (13- The volatile Keyword)
but not the race condition.
0:14
S…
Speaker 2 (13- The volatile Keyword)
So it doesn't prevent two threads simultaneously modifying some
0:18
S…
Speaker 1 (13- The volatile Keyword)
data.
0:18
S…
Speaker 2 (13- The volatile Keyword)
Instead, it ensures that if one thread changes some data,
0:22
S…
Speaker 2 (13- The volatile Keyword)
other threads can see the changes.
0:24
S…
Speaker 2 (13- The volatile Keyword)
Let me show you a real example.
0:25
S…
Speaker 2 (13- The volatile Keyword)
So here in the download status class,
0:27
S…
Speaker 2 (13- The volatile Keyword)
let's add a new field.
0:29
S…
Speaker 2 (13- The volatile Keyword)
Private Boolean is done.
0:33
S…
Speaker 2 (13- The volatile Keyword)
With this flag,
0:34
S…
Speaker 2 (13- The volatile Keyword)
we can tell if you have downloaded all the files or not.
0:36
S…
Speaker 2 (13- The volatile Keyword)
Now let's create a getter and a setter for this field.
0:39
S…
Speaker 2 (13- The volatile Keyword)
So Alt and Enter,
0:40
S…
Speaker 2 (13- The volatile Keyword)
create getter and setter.
0:42
S…
Speaker 1 (13- The volatile Keyword)
Now,
0:44
S…
Speaker 2 (13- The volatile Keyword)
I prefer to rename the setter to
0:48
S…
Speaker 2 (13- The volatile Keyword)
done and remove this parameter.
0:52
S…
Speaker 2 (13- The volatile Keyword)
So here we explicitly set this field to true.
0:54
S…
Speaker 2 (13- The volatile Keyword)
We're essentially sending the done message to this object and let this
0:58
S…
Speaker 2 (13- The volatile Keyword)
object take care of setting its status.
1:01
S…
Speaker 2 (13- The volatile Keyword)
Now, in our demo class,
1:03
S…
Speaker 2 (13- The volatile Keyword)
we want to create two threads.
1:04
S…
Speaker 2 (13- The volatile Keyword)
One thread simulates downloading a file,
1:06
S…
Speaker 2 (13- The volatile Keyword)
and the other checks to see if the download is finished.
1:09
S…
Speaker 2 (13- The volatile Keyword)
When the download is finished,
1:11
S…
Speaker 2 (13- The volatile Keyword)
we want to report something to the user.
1:12
S…
Speaker 1 (13- The volatile Keyword)
So,
1:13
S…
Speaker 2 (13- The volatile Keyword)
first let's create a download status object,
1:16
S…
Speaker 2 (13- The volatile Keyword)
download status.
1:18
S…
Speaker 2 (13- The volatile Keyword)
Now we create our first thread,
1:21
S…
Speaker 2 (13- The volatile Keyword)
so thread one,
1:22
S…
Speaker 2 (13- The volatile Keyword)
we set this to a new thread with a new instance of our download
1:26
S…
Speaker 2 (13- The volatile Keyword)
file task,
1:27
S…
Speaker 2 (13- The volatile Keyword)
and pass our status object.
1:29
S…
Speaker 1 (13- The volatile Keyword)
Now,
1:30
S…
Speaker 2 (13- The volatile Keyword)
Back to our download file task.
1:32
S…
Speaker 2 (13- The volatile Keyword)
I want to run this loop for 1
1:36
S…
Speaker 2 (13- The volatile Keyword)
million times because I don't want this to finish so quickly.
1:39
S…
Speaker 1 (13- The volatile Keyword)
Now,
1:40
S…
Speaker 2 (13- The volatile Keyword)
after this loop,
1:41
S…
Speaker 2 (13- The volatile Keyword)
we're going to call status .don.
1:44
S…
Speaker 2 (13- The volatile Keyword)
We're sending the don message to our status object.
1:48
S…
Speaker 1 (13- The volatile Keyword)
Now,
1:49
S…
Speaker 2 (13- The volatile Keyword)
back to our demo class.
1:50
S…
Speaker 2 (13- The volatile Keyword)
We create our second thread,
1:52
S…
Speaker 2 (13- The volatile Keyword)
so var thread2.
1:54
S…
Speaker 2 (13- The volatile Keyword)
We create a new thread.
1:58
S…
Speaker 2 (13- The volatile Keyword)
Now this time,
1:59
S…
Speaker 2 (13- The volatile Keyword)
I want to pass a lambda expression to represent a runnable
2:03
S…
Speaker 1 (13- The volatile Keyword)
object.
2:03
S…
Speaker 2 (13- The volatile Keyword)
We could also create a separate class that implements the runnable interface,
2:06
S…
Speaker 2 (13- The volatile Keyword)
but I want to change things up for a little bit here.
2:09
S…
Speaker 1 (13- The volatile Keyword)
So,
2:09
S…
Speaker 2 (13- The volatile Keyword)
you pass a lambda expression for the runnable interface.
2:13
S…
Speaker 2 (13- The volatile Keyword)
That's basically a function with no parameters that
2:17
S…
Speaker 2 (13- The volatile Keyword)
returns void.
2:18
S…
Speaker 1 (13- The volatile Keyword)
Now,
2:19
S…
Speaker 2 (13- The volatile Keyword)
in the body of this lambda,
2:21
S…
Speaker 2 (13- The volatile Keyword)
we should continuously ask this status object if it's done or not.
2:25
S…
Speaker 2 (13- The volatile Keyword)
So, we had a while loop
2:27
S…
Speaker 2 (13- The volatile Keyword)
We ask the status object.
2:28
S…
Speaker 2 (13- The volatile Keyword)
Are you done?
2:29
S…
Speaker 2 (13- The volatile Keyword)
As long as it's not done,
2:31
S…
Speaker 2 (13- The volatile Keyword)
we're going to wait.
2:32
S…
Speaker 2 (13- The volatile Keyword)
So here we have a while loop with an empty body.
2:35
S…
Speaker 2 (13- The volatile Keyword)
We're just waiting.
2:36
S…
Speaker 2 (13- The volatile Keyword)
The moment our download is finished,
2:38
S…
Speaker 2 (13- The volatile Keyword)
you're going to print status dot get total bytes.
2:43
S…
Speaker 1 (13- The volatile Keyword)
Now,
2:46
S…
Speaker 2 (13- The volatile Keyword)
let's start both these threads.
2:48
S…
Speaker 2 (13- The volatile Keyword)
So thread one dot start and thread two dot
2:52
S…
Speaker 1 (13- The volatile Keyword)
start.
2:52
S…
Speaker 2 (13- The volatile Keyword)
Now run our program.
2:55
S…
Speaker 1 (13- The volatile Keyword)
So
2:57
S…
Speaker 2 (13- The volatile Keyword)
Thread 1 started and finished,
2:59
S…
Speaker 2 (13- The volatile Keyword)
but thread 2 is still going.
3:00
S…
Speaker 2 (13- The volatile Keyword)
In fact, it's going to go forever.
3:01
S…
Speaker 2 (13- The volatile Keyword)
Why is this happening?
3:03
S…
Speaker 2 (13- The volatile Keyword)
Well, let me stop the program first.
3:05
S…
Speaker 2 (13- The volatile Keyword)
Back to our download status class.
3:09
S…
Speaker 2 (13- The volatile Keyword)
Earlier we used the synchronous keyword in two places
3:13
S…
Speaker 2 (13- The volatile Keyword)
in this class.
3:13
S…
Speaker 2 (13- The volatile Keyword)
So how come the synchronous keyword didn't help us here?
3:16
S…
Speaker 1 (13- The volatile Keyword)
Well,
3:17
S…
Speaker 2 (13- The volatile Keyword)
the synchronous keyword is not a silver bullet.
3:19
S…
Speaker 2 (13- The volatile Keyword)
In this particular case,
3:21
S…
Speaker 2 (13- The volatile Keyword)
we want to make sure that only one thread at a time can update this field.
3:24
S…
Speaker 2 (13- The volatile Keyword)
But here we're dealing
3:26
S…
Speaker 2 (13- The volatile Keyword)
with another field.
3:28
S…
Speaker 2 (13- The volatile Keyword)
These are two separate fields.
3:29
S…
Speaker 1 (13- The volatile Keyword)
Now,
3:30
S…
Speaker 2 (13- The volatile Keyword)
if you want to solve the problem in this demo,
3:31
S…
Speaker 2 (13- The volatile Keyword)
we should mark these two methods as synchronized.
3:35
S…
Speaker 2 (13- The volatile Keyword)
I told you that this is a bad practice.
3:37
S…
Speaker 2 (13- The volatile Keyword)
You should prefer to use the synchronized block as opposed to declaring an entire method
3:41
S…
Speaker 2 (13- The volatile Keyword)
synchronized,
3:42
S…
Speaker 2 (13- The volatile Keyword)
but let's not worry about the problem with this approach and see if the synchronized
3:46
S…
Speaker 2 (13- The volatile Keyword)
keyword solves the problem.
3:47
S…
Speaker 1 (13- The volatile Keyword)
Now,
3:48
S…
Speaker 2 (13- The volatile Keyword)
run the program.
3:51
S…
Speaker 2 (13- The volatile Keyword)
So, thread 1 started,
3:52
S…
Speaker 2 (13- The volatile Keyword)
it finished,
3:53
S…
Speaker 2 (13- The volatile Keyword)
thread 2 got notified and printed the total number of bytes.
3:57
S…
Speaker 2 (13- The volatile Keyword)
So the problem is fixed.
3:59
S…
Speaker 1 (13- The volatile Keyword)
Now,
4:00
S…
Speaker 2 (13- The volatile Keyword)
back to our demo class.
4:02
S…
Speaker 2 (13- The volatile Keyword)
Even though the synchronous keyword solved this problem,
4:04
S…
Speaker 2 (13- The volatile Keyword)
it can cause a lot of overhead,
4:06
S…
Speaker 2 (13- The volatile Keyword)
because in this while loop,
4:08
S…
Speaker 2 (13- The volatile Keyword)
we're constantly calling the isDone method.
4:11
S…
Speaker 2 (13- The volatile Keyword)
While we're calling this method,
4:13
S…
Speaker 2 (13- The volatile Keyword)
no other threads can do anything else with this status object.
4:16
S…
Speaker 2 (13- The volatile Keyword)
They cannot call any other methods in this object.
4:18
S…
Speaker 2 (13- The volatile Keyword)
So all these method calls will run in sequence.
4:21
S…
Speaker 2 (13- The volatile Keyword)
That will show you a better way.
4:23
S…
Speaker 1 (13- The volatile Keyword)
So,
4:24
S…
Speaker 2 (13- The volatile Keyword)
back to our download status,
4:25
S…
Speaker 2 (13- The volatile Keyword)
let's remove the synchronous keyword.
4:27
S…
Speaker 1 (13- The volatile Keyword)
Back to the
4:31
S…
Speaker 2 (13- The volatile Keyword)
original problem.
4:32
S…
Speaker 2 (13- The volatile Keyword)
When we run this program,
4:34
S…
Speaker 2 (13- The volatile Keyword)
the second thread waits indefinitely because
4:38
S…
Speaker 2 (13- The volatile Keyword)
it doesn't see the change to the is done field.
4:42
S…
Speaker 2 (13- The volatile Keyword)
This is the visibility problem.
4:45
S…
Speaker 2 (13- The volatile Keyword)
So one thread changes some data,
4:47
S…
Speaker 2 (13- The volatile Keyword)
the other thread cannot see the change.
4:49
S…
Speaker 2 (13- The volatile Keyword)
But why does this happen?
4:50
S…
Speaker 1 (13- The volatile Keyword)
Well,
4:51
S…
Speaker 2 (13- The volatile Keyword)
the Java virtual machine makes some optimizations under the hood to make our code
4:55
S…
Speaker 2 (13- The volatile Keyword)
run faster.
4:56
S…
Speaker 2 (13- The volatile Keyword)
One of these optimizations is caching values.
4:59
S…
Speaker 2 (13- The volatile Keyword)
So let's say we have an integer field with the value 1.
5:02
S…
Speaker 2 (13- The volatile Keyword)
This value is stored in the main memory or RAM.
5:05
S…
Speaker 1 (13- The volatile Keyword)
Now,
5:06
S…
Speaker 2 (13- The volatile Keyword)
we have two threads running by two different CPU cores.
5:09
S…
Speaker 2 (13- The volatile Keyword)
Each CPU has a cache,
5:11
S…
Speaker 2 (13- The volatile Keyword)
which is a small amount of memory available locally in that CPU.
5:15
S…
Speaker 2 (13- The volatile Keyword)
Reading the data from the cache is faster because the data is closer to
5:19
S…
Speaker 2 (13- The volatile Keyword)
the CPU.
5:19
S…
Speaker 2 (13- The volatile Keyword)
So it doesn't have to travel far.
5:21
S…
Speaker 2 (13- The volatile Keyword)
It doesn't have to travel between the CPU and the main memory.
5:24
S…
Speaker 1 (13- The volatile Keyword)
Okay.
5:24
S…
Speaker 2 (13- The volatile Keyword)
Now,
5:25
S…
Speaker 2 (13- The volatile Keyword)
this is what happens.
5:27
S…
Speaker 2 (13- The volatile Keyword)
Two threads read the value of a field and store it locally.
5:30
S…
Speaker 1 (13- The volatile Keyword)
Now,
5:31
S…
Speaker 2 (13- The volatile Keyword)
the first thread changes its value,
5:33
S…
Speaker 2 (13- The volatile Keyword)
but this change is only local to this thread.
5:35
S…
Speaker 2 (13- The volatile Keyword)
So the second thread doesn't see the change.
5:37
S…
Speaker 2 (13- The volatile Keyword)
Even if it writes the change back in the memory,
5:40
S…
Speaker 2 (13- The volatile Keyword)
the second thread doesn't see the change because it already has the value
5:44
S…
Speaker 2 (13- The volatile Keyword)
of this field in its cache.
5:45
S…
Speaker 2 (13- The volatile Keyword)
This is the visibility problem.
5:47
S…
Speaker 2 (13- The volatile Keyword)
To solve this problem without the overhead of synchronization,
5:51
S…
Speaker 2 (13- The volatile Keyword)
we can declare this field as volatile.
5:54
S…
Speaker 2 (13- The volatile Keyword)
So we type volatile.
5:57
S…
Speaker 2 (13- The volatile Keyword)
volatile means unstable.
5:58
S…
Speaker 2 (13- The volatile Keyword)
So we're telling the Java virtual machine,
6:00
S…
Speaker 2 (13- The volatile Keyword)
hey, this field is unstable.
6:03
S…
Speaker 2 (13- The volatile Keyword)
It may change.
6:04
S…
Speaker 2 (13- The volatile Keyword)
So don't rely on the value stored in the cache.
6:06
S…
Speaker 2 (13- The volatile Keyword)
Always read it from the main memory.
6:08
S…
Speaker 2 (13- The volatile Keyword)
And that means if another thread updates the value of this field,
6:12
S…
Speaker 2 (13- The volatile Keyword)
the change will be immediately written to the main memory.
6:15
S…
Speaker 2 (13- The volatile Keyword)
Now let's run a program and make sure the problem is solved.
6:19
S…
Speaker 1 (13- The volatile Keyword)
So,
6:19
S…
Speaker 1 (13- The volatile Keyword)
run.
6:20
S…
Speaker 1 (13- The volatile Keyword)
There you go.
6:25
S…
Speaker 2 (13- The volatile Keyword)
So the volatile keyword guarantees that the changes to a field
6:29
S…
Speaker 2 (13- The volatile Keyword)
is visible across threads.
هيءَ ترانسڪريٽ AI (آٽوميٽڪ سڏ سڃاڻپ) پاران تيار ڪئي وئي آھي. ان ۾ غلطيون ٿي سگھن ٿيون - اصل آڊيو سان چيڪ ڪريو ته جيئن خطرناڪ استعمال ڪري سگھجي. AI پاليسي
خلاصو
ھن ترانسڪريپٽ جي AI خلاصي پيدا ڪرڻ لاءِ خلاصو دٻايو.
خلاصو ڪيو وڃي ٿو...
AI کان ان ترانسڪريپٽ بابت پڇو
ھن ترانسڪريپشن بابت ڪابه سوال ڪريو - AI لاڳاپيل حصا ڳوليندو ۽ جواب ڏيندو.