如何在Java中使用等待和通知而没有非法监控状态异常?

我有2个矩阵,我需要将它们相乘,然后打印每个单元格的结果。一旦一个单元格准备就绪,我就需要打印它,但例如,我需要在单元格[2][0]之前打印[0]单元格,即使[2][0]的结果首先准备就绪。所以我需要按订单打印它。所以我的想法是让打印机线程等待,直到通知它正确的单元格已准备好打印,然后将打印单元格并返回等待等等。multiplyThreadprinterThread

所以我有这个线程来做乘法:

public void run() 
{
    int countNumOfActions = 0; // How many multiplications have we done
    int maxActions = randomize(); // Maximum number of actions allowed

    for (int i = 0; i < size; i++)
    {       
        result[rowNum][colNum] = result[rowNum][colNum] + row[i] * col[i];
        countNumOfActions++;
        // Reached the number of allowed actions
        if (countNumOfActions >= maxActions)
        {
            countNumOfActions = 0;
            maxActions = randomize();
            yield();
        }   
    }
    isFinished[rowNum][colNum] = true;
    notify();
}

打印每个单元格结果的线程:

public void run()
{
    int j = 0; // Columns counter
    int i = 0; // Rows counter
    System.out.println("The result matrix of the multiplication is:");

    while (i < creator.getmThreads().length)
    {
        synchronized (this)
        {
            try 
            {
                this.wait();
            } 
            catch (InterruptedException e1) 
            {
            }
        }
        if (creator.getmThreads()[i][j].getIsFinished()[i][j] == true)
        {
            if (j < creator.getmThreads()[i].length)
            {
                System.out.print(creator.getResult()[i][j] + " ");
                j++;
            }
            else
            {
                System.out.println();
                j = 0;
                i++;
                System.out.print(creator.getResult()[i][j] + " ");
            }
        }
    }

现在它给我带来了这些异常:

Exception in thread "Thread-9" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-6" Exception in thread "Thread-4" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at multiplyThread.run(multiplyThread.java:49)
java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-5" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-8" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-7" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-11" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-10" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-12" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at multiplyThread.run(multiplyThread.java:49)

第 49 行是“notify()”。我认为我需要以不同的方式使用同步,但我不确定如何。multiplyThread

如果有人可以帮助这个代码工作,我会非常感激。


答案 1

为了能够调用 notify(),您需要在同一对象上进行同步。

synchronized (someObject) {
    someObject.wait();
}

/* different thread / object */
synchronized (someObject) {
    someObject.notify();
}

答案 2

在 Java 中使用 and 或 方法时,必须记住以下事项:waitnotifynotifyAll

  1. 如果您预计有多个线程将等待锁定,请使用。notifyAllnotify
  2. 必须在同步上下文中调用等待通知方法。有关更详细的说明,请参阅链接。
  3. 始终在循环中调用该方法,因为如果多个线程正在等待锁定,并且其中一个线程获得了锁定并重置了条件,则其他线程需要在唤醒后检查条件,以查看它们是否需要再次等待或可以开始处理。wait()
  4. 使用相同的对象进行调用和方法;每个对象都有自己的锁,因此调用对象 A 和对象 B 没有任何意义。wait()notify()wait()notify()