Difference between notify and notifyAll in java

In this post, we are going to see difference between notify and notifyall in java.

notify():

When you call notify method on the object, it wakes one of thread waiting for that object. So if multiple threads are waiting for an object, it will wake of one of them. Now you must be wondering which one it will wake up. It actually depends on OS implementation.

notifyAll() :

notifyAll will wake up all threads waiting on that object unlike notify which wakes up only one of them.Which one will wake up first depends on thread priority and OS implementation.
Lets understand it with the help of example:
1. Create a class named File.java:
It is java bean class on which thread will act and call wait and notify method.
2. Create a class named FileReader.java
This thread will wait until other thread call notify method, then after it will complete its processing. It will first take a lock on file object and will be called from synchronized block .So in this example, it will wait for FileWriter to complete the file.
3. Create a class named FileWriter.java
This class will notify thread(in case of notify) which is waiting on file object. It will not give away lock as soon as notify is called, it first complete its synchronized block. So in this example, FileWriter will complete the file and notify it to FileReaders.
4. Create a class NotifyAndNotifyAllMain,java.
This is our main class which will create object of above classes and run it.
In case of notify():
When you run above program, you will get following outputs:
So here,two FileReader threads(reader 1 and reader 2) are waiting for file to be completed,so they called file.wait(). Once FileWriter completes it’s file, it called file.notify() and reader 2 thread gets up and completes its processing.
In case of notifyAll() :
Lets change FileWriter class to call file.notifyAll().
When you run above program, you will get following output:
In case of notifyAll(), it notifies all threads waiting on that object.

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. Using Thread.sleep Sleep method causes current thread to pause for […]

  • 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 […]

  • wait(),notify() and notifyAll() in java
    22 October

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

    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? Let’s […]

  • 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. We will use array to store elements in […]

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.