CodeGym /Courses /Java Multithreading /Introducing the wait, notify, and notifyAll methods

Introducing the wait, notify, and notifyAll methods

Java Multithreading
Level 1 , Lesson 6
Available

"Hi, Amigo!"

"Hi, Rishi!"

"I'm going to introduce you to the Object class's wait, notify, and notifyAll methods."

"Today we'll just get acquainted with them, but we'll come back later and spend more time on this."

"Okay."

"These methods were invented as part of the thread synchronization mechanism."

"Let me remind you that Java has a built-in mechanism for controlling access to shared resources (objects) from different threads. A thread can declare that an object is busy, and other threads will have to wait until the busy object is released."

"I remember. You do that using the synchronized keyword."

"Right. Typically, the code would look something like this:"

public void print()
{
 Object monitor = getMonitor();
 synchronized(monitor)
 {
  System.out.println("text");
 }
}

"Remember how that works?"

"Yep. If two threads simultaneously call the print() method, one of them will enter the block labeled synchronized and lock the monitor, which makes it so the second thread will wait until the monitor is released."

"Right. Once a thread enters the block labeled synchronized, the monitor object is marked as busy, and other threads will be forced to wait for the monitor object to be released. The same monitor object can be used in various parts of the program."

"By the way, why did you choose the name monitor?"

"A monitor is what you usually call an object that stores the busy or free status."

"And this is where the wait and notify methods come into play."

"Actually, these are really the only two methods. The others are just adaptations of these methods."

"Now let's wrap our heads around what the wait method is and why we need it."

"Sometimes there are situations in a program where a thread enters a block of synchronized code and locks the monitor, but can't keep going because it is missing some data. For example, a file it needs to process hasn't finished downloading or something like that."

"We could just wait for the file to be downloaded. You could just check it using a loop. If the file hasn't downloaded yet, then sleep for a second or so, and check again until it is downloaded."

"Something like this:"

while(!file.isDownloaded())
{
 Thread.sleep(1000);
}
processFile(file);

"But in our case, this type of waiting is too expensive. Since our thread locked the monitor, other threads are also forced to wait even though they may already have the data they need."

"The wait() method was invented to solve this problem. This method causes the thread to release the monitor and then «suspends» the thread.

"You can only call a monitor object's wait method when the monitor is busy, i.e. only inside a synchronized block. When this happens, the thread temporarily stops running, and the monitor is released so that other threads can use it."

"There are often instances where a thread will enter a synchronized block and call wait, thus releasing the monitor."

"Then a second thread will enter and be suspended, then a third, and so on."

"And how does a thread get resumed?"

"For that, there is a second method: notify."

"You can only call a monitor object's notify/notifyAll methods when the monitor is busy, i.e. only inside a synchronized block. The notifyAll method wakes up all threads that are waiting on this monitor object."

"The notify method 'unfreezes' one random thread, but the notifyAll method unfreezes all of this monitor's «frozen» threads."

"Very interesting. Thank you, Rishi."

"There are also adaptations of the wait() method:"

wait() method Explanation
void wait(long timeout)
The thread «freezes», but it automatically «unfreezes» after waiting the number of milliseconds passed to the method as an argument.
void wait(long timeout, int nanos)
The thread «freezes», but it automatically «unfreezes» after waiting the number of nanoseconds passed to the method as an argument.

"We also call this a wait with a timeout. The method works like a normal wait, but if the specified time has passed and the thread hasn't been woken up, it wakes itself up."

Comments (15)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Ranganathan Kasiganesan Level 94, Chennai, India Expert
6 April 2024
I want to add a point here that some people may not know about the monitor. Actually "Monitor" is an important object of an Operating System such as Windows, Mac & Linux. Our Java thread takes control of this "Monitor" and controls the execution of a thread. This is important for the people to understand who have not studied Computer Science & Engineering. For them, I can suggest a book on "Operating Systems." Name: Operating System Concepts Authors: Abraham Silberschatz & Galvin
Ranganathan Kasiganesan Level 94, Chennai, India Expert
6 April 2024
Though I know already, this material nailed me very well and gave me a strong understanding of the usage of the monitor and timed wait.
Kenny Level 93, Brussieu, India
29 October 2023
Why teach us this when we haven't learned about thread yet ?
Hoist Level 16, San Diego, United States
30 October 2023
IMO --- codegym uses out of order execution as a learning tool. lol If you dig around inside here using internal search - read some HELP / Articles and get into some Forums -- then the magic happens. Tons of really good articles within here for sure.
Evgeniia Shabaeva Level 42, Budapest, Hungary
2 December 2024
I believe there were a few lessons about threads in Java Core here.
阿狼 Level 32, Zhengzhou, China
1 July 2022
d24
Andrei Level 41
22 March 2021
Vibe check: relax baby, you're on the right path, keep on keepin'. 💪🤙
myqms Level 22, Landshut, Germany
21 June 2020
Where does getMonitor() come from? Object monitor = getMonitor();
Fadi Alsaidi Level 34, Carrollton, TX, USA
2 August 2020
It's just made up for understanding. You can't getMonitor or the state of the object's Monitor( locked/unlocked). You will learn about it later in one of Dr. Noodles additional reading articles
Adam Odoj Level 22, Poland
25 March 2023
Thanks, but it is more confusing. I was searching all resources to find this method...😏
Johannes Level 27, Centurion, Pretoria, South-Africa
24 April 2020
Sounds easy, but dreading the exercises, LOL! Wonder why there would be a need for "wait(long timeout)" ? It's no different from "sleep()" ?
Lawson Level 29, Lagos, Nigeria
9 September 2020
i wonder also... aint they all the same? the sleep method and the wait method
Mateusz Level 29, Poland
22 September 2020
As far as I get it, the wait method freezes the thread but also allows other threads to enter the synchronized block. The sleep method would only freeze the thread without allowing others to enter the block.
thinhnguyen Level 24, Danang, Viet Nam
21 September 2019
It is easy to understand
Azja Level 32, Krakow, Poland
9 May 2019
Very interesting. Thank you, Rishi