简体   繁体   English

Java线程notify()方法

[英]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() ? 现在有一种机会在这种情况下正在等待,我真的需要调用notify()吗? I think as synchronized block execution completes other thread will release the lock ? 我认为随着同步块执行的完成,其他线程会释放锁吗? what is the exact use of notify() method ? notify()方法的确切用途是什么?

Threading experts please explain ? 请线程专家解释一下?

No, you wouldn't need to use notify() in that case. 不,在那种情况下,您不需要使用notify() 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. notify()方法与wait()方法结合使用。 When one thread invokes wait() , it may release the lock and begin waiting. 当一个线程调用wait() ,它可能释放锁并开始等待。 One of the conditions that can end the wait is when another thread invokes notify() . 可以终止等待的条件之一是另一个线程调用notify() Both wait() and notify() must be invoked on an instance on which the current thread is synchronized . 必须在synchronized当前线程的实例上调用wait()notify()

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. 如果消费者用尽了要处理的信息,则它可能会wait()直到生产者执行notify()有更多数据可用。

当线程进入同步块并调用等待时,进入同步块时释放的锁被释放,并且该线程等待其他线程通知它,在这种情况下它将重新获取该锁并继续操作。当线程退出时再次释放锁同步块。

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. 线程通过调用其中一个wait方法在对象的监视器上等待。

so if an object is waiting by calling a wait method. 因此,如果对象正在通过调用wait方法来wait then you can awake them using notify. 那么您可以使用notify唤醒它们。

Description 描述

The java.lang.Object.notify() wakes up a single thread that is waiting on this object's monitor. java.lang.Object.notify()唤醒在此对象的监视器上等待的单个线程。 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. 线程通过调用其中一个wait方法在对象的监视器上等待。

For more information refer below links. 有关更多信息,请参见下面的链接。

notify() 通知()

Documentation 文档

Doc 文件

I hope it will help you. 希望对您有帮助。

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: 并且如果任何线程进行了任何更改,则使用下面的notify方法对所有线程可见,这是示例代码:

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);

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM