简体   繁体   English

Java等待,通知-等待不起作用?

[英]Java wait, notify - wait not working?

I'm learning work with Thread in Java . 我正在学习Java Thread I am trying to write a program that will have two threads. 我正在尝试编写一个具有两个线程的程序。 Each thread will write your name in the cycle. 每个线程都会在循环中写下您的名字。 In Main run cycle will generate numbers ( 0 , 9 ). Main run cycle中将生成数字(0,9)。 When it generates an odd number, wait the thread 1 ( even - thread 2 ) . 当它生成奇数时,请等待线程1(偶数线程2)。 If a thread will sleep and wake him up . 如果一个线程会睡觉并唤醒他。 The problem is that when I call wait , the program freezes . 问题是当我调用wait时,程序冻结。 Code: 码:

import java.util.Random;

public class TestClass {


    public static void main(String[] args) throws InterruptedException {

        myThread one = new myThread("Thread one");
        myThread two = new myThread("Thread two");

        one.start();
        two.start();

        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            int rnd = random.nextInt(10);
            if (rnd % 2 == 0) {
                synchronized (two) {
                    two.wait(); // first loop - freez
                }
            } else {
                synchronized (one) {
                    one.wait();
                }
            }
        }

    }

}

class myThread extends Thread {

    private boolean canIRun = true;
    private final String name;

    myThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (canIRun) {
            System.out.println("My name is: " + name);
        }
    }

}

You have three threads in your program, Main thread is where the program starts executing by entering the main() method. 您的程序中有三个线程, Main thread是通过输入main()方法开始执行程序的位置。 Main thread creates two threads namely Thread One and Thread Two . Main thread创建两个线程,即线程一线程二 Thread one and two runs their run() method. 线程一和线程二运行它们的run()方法。 Main thread is running main() method. Main thread正在运行main()方法。 When you call one.wait() in main() method, it pauses the current thread, which is Main thread . 当您在main()方法中调用one.wait()时,它将暂停当前线程,即Main thread It wont pause Thread one . 它不会暂停Thread one the same is true for two.wait() , it pauses the Main thread too. two.wait()也是如此,它也会暂停Main thread Now the Main thread can only resume, if any other thread calls notify() or notifyAll() on the same object either one or two , whichever object made it to suspend. 现在, Main thread只能重新开始,如果任何其他线程调用notify()notifyAll()相同的对象或者在onetwo ,取其对象使它暂停。

In your program, Main thread freezes, but the other two thread executes continuously until the program is terminated. 在您的程序中, Main thread冻结,但是其他两个线程连续执行,直到程序终止。

I wanted to create an answer that re-broadcasts @jonskeet's comment above: 我想创建一个答案,重新广播上面的@jonskeet的评论:

Do not call wait() or notify() on Thread objects - it's unfortunate, but Thread uses its own monitor for other things. 请勿在Thread对象上调用wait()或notify()-这很不幸,但是Thread使用自己的监视器进行其他操作。

I have found this out several times the hard way in classes that extend Thread or implement Runnable that were hung because of synchronized(this). 我在扩展Thread或实现由于sync(this)挂起的Runnable的类中的困难方式中多次发现了这一点。 Since finding this question, I have searched many times and in nowhere have I found any warning to not do this. 自发现此问题以来,我进行了许多次搜索,但从未发现过任何警告不这样做的警告。 And if you have a fast, idle computer, you will probably never find this in testing. 而且,如果您有一台快速闲置的计算机,则可能在测试中永远找不到。

Perhaps someone with better google-foo can find the documentation. 也许拥有更好google-foo的人可以找到该文档。 If so, please comment or amend this answer. 如果是这样,请评论或修改此答案。

wait notify threads synchronized this hung thread 等待通知线程同步此挂起的线程

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

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