Why wait(), notify() And notifyAll() methods are in Object Class

wait(),notify() and notifyAll() in java

In this post, we will see why wait(), notify() And notifyAll() methods are in Object Class And Not in Thread Class.

This is one of the most asked java multithreading interview questions.

You might know that wait(), notify() And notifyAll() Methods are in Object class and do you know the reason for the same?

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Let’s discuss why wait(), notify() And notifyAll() Methods Are in Object Class.

In Java, thread waits on monitor assigned to the object and when you want to send a signal to another thread who is waiting for the same monitor, you call notify() method to wake one thread and notifyAll() to wake up all the threads.

If wait, notify and notifyAll method are in thread class, then each thread should be aware of the status of another thread.

For example: Let’s say you have two threads, T1 and T2. Now T1 must know that T2 was waiting for this particular resource which I have just freed because T1 will need to call T2.notify().

In Java, the object itself is shared among the threads and facilitates inter-thread communication. Threads have no specific knowledge of each other. They can run asynchronously and are independent. They just run, lock, wait and get notified. They do not need to know about the status of other threads. They just need to call notify method on an object, so whomever thread is waiting on that resource will be notified.

As stated before,

  • When you call wait() method on the object, then it gives up monitor and go to sleep
  • When you call notify() method, then the single thread which is waiting for the object’s monitor will be notified.

Hence wait, notify() And notifyAll() work at object’s monitor level. If thread which is currently holding the monitor, wants to give up the monitor then it will call wait method on the object and if it want to notify other thread, then it will call notify method on the object.

Shared objects allow threads to communicate by calling wait(), notify() And notifyAll() Methods, so these methods are in the object class.

That’s all about why wait(), notify() And notifyAll() methods Are in Object Class And Not in Thread Class.


import_contacts

You may also like:

Related Posts

  • Get Thread Id in Java
    30 September

    Get Thread Id in Java

    In this article, we will learn to get thread id of a running thread in Java. An Id is a unique positive number generated at the time of thread creation. This id remains unchanged during the lifetime of the thread. When a thread gets terminated its id can be used to refer another thread, but […]

  • ArrayBlockingQueue in java
    19 September

    ArrayBlockingQueue in java

    In this article, we will understand the Java concurrent queue, BlockingQueue. We will then go deep into it’s one of the implementation, ArrayBlockingQueue. What is BlockingQueue BlockingQueue interface was introduced in Java 5 under concurrent APIs and it represents a thread-safe queue in which elements can be added to and removed from. We can have […]

  • Delay java program by few seconds
    11 December

    Delay Java program for few secs

    In this post, we will see how to delay java program for few secs. We need to delay a java programs in many situation where we want to wait for some other task to finish. There are multiple ways to delay execution of java program. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go […]

  • 29 May

    How to print even and odd numbers using threads in java

    In this post, we will see how to print even and odd numbers using threads in java. see also: How to print sequence using 3 threads in java Problem You are given two threads. You need to print odd numbers using one thread and even numbers using another thread.You need to print in natural order […]

  • Custom BlockingQueue in java
    16 October

    Custom BlockingQueue implementation in java

    In this post, we will see how to create your own custom BlockingQueue. This is one of the most asked java interview questions. You need to implement your own BlockingQueue. This question helps interviewer to get your understanding of multithreading concepts. Here is simple implementation of BlockingQueue. 💻 Awesome Tech Resources: Looking for ⚒️ tech […]

  • Difference between Runnable and Callable in java
    16 December

    Difference between Runnable and Callable in java

    Runnable and Callable interface both are used in the multithreading environment.Callable is available in java.util.concurrent.Callable package and Runnable in java.lang.Thread. Difference between Runnable and Callable interface in java Runnable was introduced in java 1.0 version While Callable is an extended version of Runnable and introduced in java 1.5 to address the limitation of Runnable. Runnable […]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.