Malatesh
Malatesh

Reputation: 1954

Java threading notify() method

Consider there are two threads which are waiting to execute synchronized block. Now one get chance one is waiting in this case do I really need to call notify() ? I think as synchronized block execution completes other thread will release the lock ? what is the exact use of notify() method ?

Threading experts please explain ?

Upvotes: 0

Views: 95

Answers (5)

erickson
erickson

Reputation: 269637

No, you wouldn't need to use notify() in that case. You are correct, the thread that had to wait to acquire the lock would automatically proceed after the lock was released.

The notify() method works in conjunction with the wait() method. When one thread invokes wait(), it may release the lock and begin waiting. One of the conditions that can end the wait is when another thread invokes notify(). Both wait() and notify() must be invoked on an instance on which the current thread is synchronized.

This can be used, for example, to create a channel between two threads, where one thread is consuming information produced by another. If the consumer runs out of information to process, it might wait() until the producer does a notify() that more data are available.

Upvotes: 1

Simmant
Simmant

Reputation: 1513

Like if you are using Multiple threads the method is synchronized which means it will share among all the threads but any thread will use it after the execution another thread. and if there is any change is made by any thread then it will visible for all by using notify method below code is example for that:

class Detail {
    public String name = "", sername = "";
    Scanner sc;

    public synchronized String getData() {

        try {
            wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return name+""+sername;
    }

    public synchronized void show() {
        try {
            name = "hello";
            sername = "hii";
            Thread.sleep(1000);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        notify();


    }

}

class Data1 extends Thread {
    Detail detail;
    public Data1(Detail detail1)
    {
    //  super("1");
        this.detail = detail1;
        start();
    }

    public void run()
    {
        System.out.println("name is :"+detail.getData());

    }
}

class Data2 extends Thread {
    Detail detail2;
    public Data2(Detail detail1)
    {
        //super("2");
        this.detail2 = detail1;
        start();
    }

    public void run()
    {
        detail2.show();

    }
}

public class SyncDemo {

    public static void main(String ar[])
    {
        Detail det = new Detail();
        Data1 d1=  new Data1(det);
        Data2 d2=  new Data2(det);

    }

}

Upvotes: 0

Govinda Rajbhar
Govinda Rajbhar

Reputation: 3034

Description

The java.lang.Object.notify() wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

For more information refer below links.

notify()

Documentation

Doc

I hope it will help you.

Upvotes: 0

stinepike
stinepike

Reputation: 54672

from the doc,

Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.

so if an object is waiting by calling a wait method. then you can awake them using notify.

Upvotes: 0

Aniket Thakur
Aniket Thakur

Reputation: 68905

When a thread enter synchronized block and calls wait the lock acquired while entering the synchronized block is released and the thread waits for other thread to notify it in which case it will reacquire the lock and proceed.Lock is again released when the thread comes out of the synchronized block.

Upvotes: 0

Related Questions