14- Thread Signalling with wait() and notify()
Jul 03, 2026 22:35
· 3:35
· English
· Whisper Turbo
· 2 스피커
이 증명서는 오늘 만료됩니다.
영구 저장소 업그레이드 →
보이는 것만
0:03
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Sometimes we want one thread to wait until some condition becomes true.
0:07
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
For example,
0:08
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
over here, the second thread has to wait until the done field becomes
0:12
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
true.
0:13
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Now we're implementing this using a while loop with an empty body.
0:17
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
The problem with this implementation is that it wastes CPU cycles.
0:21
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Because this while loop is continuously running until the done field becomes
0:25
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
true. It may run millions or even trillions of times.
0:29
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So it wastes CPU cycles.
0:31
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Now we can solve this problem by using the wait and notify method.
0:34
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So every object in Java has this wait and notify method.
0:38
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Let me show you.
0:38
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So here if you type status dot,
0:41
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
take a look.
0:42
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
every object has these methods notify all notify and
0:47
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
wait. These methods are inherited from the object class,
0:50
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
which is the parent of all classes in Java.
0:52
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So here we call status dot wait.
0:56
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
And this will make this thread go to sleep until another thread notifies
1:00
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
this thread that the state of the status object is changed.
1:03
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So when we call the wait method,
1:05
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
this while loop is not going to run a million times.
1:08
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
In the first iteration,
1:09
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
this wait method is going to put this thread into sleep until another
1:14
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
thread wakes it up.
1:15
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Now here we need to handle the uninterrupted exception just like before.
1:18
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So let's wrap it inside a try catch block.
1:22
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So this is the body of our while loop.
1:25
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Now the Java virtual machine expects us to call the wait method
1:29
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
inside a synchronized block.
1:32
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So we need to wrap this inside the synchronized block and
1:36
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
we're going to use the status object as our monitor.
1:39
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Now let's move the try catch block over here.
1:43
S…
Speaker 2 (14- Thread Signalling with wait() and notify())
Good.
1:44
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So this is one part of the story.
1:47
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Now we need to go to our first thread in our download
1:51
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
file task class.
1:53
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
After we call status dot done,
1:55
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
we need to notify the other thread that the state of this object is
1:59
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
changed.
1:59
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So here we call status dot notify.
2:03
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
We also have notify all.
2:06
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
This is useful if you have multiple threads waiting for a change on this
2:10
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
object.
2:10
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
With this,
2:11
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
we can notify them all.
2:12
S…
Speaker 2 (14- Thread Signalling with wait() and notify())
Now,
2:13
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
just like the wait method,
2:14
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
we need to wrap this line inside a synchronized block.
2:18
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Otherwise, the Java virtual machine is going to throw a runtime exception.
2:21
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So let's add
2:23
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
a synchronized block.
2:24
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
We're going to use the status object as the monitor and then
2:28
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
call the notify all method.
2:30
S…
Speaker 2 (14- Thread Signalling with wait() and notify())
Now,
2:32
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
with this implementation,
2:33
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
the moment the second thread gets started,
2:36
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
this while loop is going to start and in the first iteration,
2:40
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
the wait method is going to put this thread into a sleep.
2:43
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So the while loop is not going to get executed a million times.
2:47
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Now, the moment the first thread finishes its job,
2:50
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
it's going to notify the second thread.
2:51
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So the second thread is going to wake up and then it's going to print the
2:56
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
total bytes.
2:57
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Let's take a look.
2:58
S…
Speaker 2 (14- Thread Signalling with wait() and notify())
There you go.
3:02
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So this is a more efficient implementation than a wide loop with an empty body that
3:06
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
runs a million times.
3:07
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
Having said that,
3:08
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
wait and notify can get pretty tricky in complex applications.
3:12
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
If you don't use them properly,
3:14
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
you're going to run into all sorts of weird issues.
3:16
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
So I would say don't use them in new code.
3:18
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
These days we have better tools for implementing this kind of communication
3:22
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
between threads.
3:23
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
We'll look at them in the next section.
3:25
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
I just wanted to briefly talk about wait and notify methods in case you
3:29
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
have seen them before and you were curious.
3:31
S…
Speaker 1 (14- Thread Signalling with wait() and notify())
what they are and how they work.
이 기록은 AI (자동 음성 인식)에 의해 생성되었습니다. 오류가 있을 수 있습니다 - 중요한 사용을 위해 원본 오디오와 확인하십시오. AI 정책
요약
요약 을 클릭하여 이 녹음의 AI 요약을 생성합니다.
요약...
이 녹음에 대해 AI에게 물어보기
이 녹음에 대해 질문하십시오 — AI는 관련 섹션을 찾아 답변합니다.