简体   繁体   English

等待并通过实施Runnable和扩展线程示例进行通知

[英]wait and notify with implementing Runnable and extending Thread example

I am implementing the logic to understand wait and notify works i found something which i am not able to analyze when i am extending the class with thread then wait and notify is working as expected but when i am implementing the runnable interface then output will be differ 我正在实现理解等待和通知工作的逻辑,我发现了一些我用线程扩展类时无法分析的东西,然后等待和通知按预期工作,但是当我实现可运行接口时,输出将有所不同

public class AdderThread extends Thread {

int total;

@Override
public void run() {

    synchronized (this) {
        for (int i = 0; i < 1000; i++) {
            total += i;
        }

        notify();
    }

}





public class WaitNotify {

public static void main(String[] args) {

    AdderThread addrTh = new AdderThread();
    addrTh.start();
    // -1243309312

    System.out.println("Total without Wait() " + addrTh.total);


}

} }

When Implementing Runnable gives me correct answer means it is waitng for the thread to complete the task but when i am using extends thread it is giving me answer as 0 ie it is not waiting for thread to complete the task. 当实现Runnable给我正确答案时,它等待线程完成任务,但是当我使用扩展线程时,给我答案为0,即它不等待线程完成任务。

public class WaitNotify {

public static void main(String[] args) {

    AdderThread addrTh = new AdderThread();
    addrTh.start();
    // -1243309312

    System.out.println("Total Before Wait " + addrTh.total);
    synchronized (addrTh) {

        try {
            System.out.println("Waiting for Sum...");
            addrTh.wait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Total " + addrTh.total);
    }

}

} }

Your new thread can call notify() before the main thread calls `wait()." The wait() call will wait forever in that case because notify() does not do anything at all if some other thread is not already waiting for it. 您的新线程可以在主线程调用“ wait()”之前调用notify() 。在这种情况下,wait()调用将永远等待,因为如果其他线程尚未等待,则notify()根本不执行任何操作。 。

wait() and notify() are low-level operations that are meant to be used in a very specific way: wait()notify()是低级操作,应以非常特定的方式使用:

See Java wait() does not get waked by notify() for more information. 有关更多信息,请参见Java wait()不会被notify()唤醒


The difference between the case where you implement Runnable and the case where you extend Thread is due to the fact that this refers to an anonymous inner class in the first case, and it refers to the Thread object in the latter case. 其中要实现的情况之间的差异Runnable以及在哪里延伸的情况下Thread是由于这样的事实, this是指在第一种情况一个匿名内部类,它指的是Thread在后一种情况下的对象。

That matters because the thread machinery uses wait() and notify() for its own purposes. 这很重要,因为线程机制将wait()和notify()用于其自身目的。 So if your wait() call comes too late to be waked up by the notify() call in your code, it may still get waked up by a notify() in library code (eg, when the thread terminates). 因此,如果您的wait()调用来不及被代码中的notify()调用唤醒,它可能仍会被库代码中的notify()唤醒(例如,线程终止时)。 That won't happen when you implement Runnable because there's no reason why the library would ever notify it. 当您实现Runnable时,不会发生这种情况,因为没有理由为什么库会通知它。

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

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