java - IllegalMonitorStateException notify() 和 wait()

标签 java wait notify illegalmonitorstateexcep

<分区>

我有一个问题。当我在同步块(synchronized block)中使用 notify() 时,我得到 IllegalMonitorStateException。谁能帮我解决这个问题?

我需要一个线程向第二个线程发送一个字符,然后这个线程必须等待,第二个线程打印这个字符。在第二个线程等待之后,第一个线程再次发送下一个字符

主要.java:

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
public class Main extends JFrame {

    Thread t1, t2;
    Consumer con;

    public Main() {
        con = new Consumer();
        startThreads();
    }

    private synchronized void startThreads() {
        t1 = new Thread(new Producent("grudzien", con));
        t1.start();
        t2 = new Thread(con);
        t2.start();
    }

    public class Producent implements Runnable {

        String m_atom;
        char[] atoms;
        Consumer m_c;

        public Producent(String atom, Consumer c) {
            m_atom = atom;
            m_c = c;
        }

        @Override
        public void run() {
            synchronized (this) {
                atoms = m_atom.toCharArray();
                System.out.print("Tablica znaków: ");
                for (int i = 0; i < atoms.length; i++) {
                    System.out.print(atoms[i] + ", ");
                }
            }
            for (int i = 0; i < atoms.length; i++) {
                synchronized (this) {
                    con.setChar(atoms[i]);
                    t2.notify();
                    try {
                        wait();
                    } catch (InterruptedException ex) {
                        JOptionPane.showMessageDialog(null, "Blad w wait()", "Blad!", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }

        }
    }

    public class Consumer implements Runnable {

        char atom;

        public void setChar(char c) {
            atom = c;
        }

        @Override
        public void run() {
            while (true) {
                synchronized (this) {
                    try {
                        wait();
                    } catch (InterruptedException ex) {
                        JOptionPane.showMessageDialog(null, "Blad w wait()", "Blad!", JOptionPane.ERROR_MESSAGE);
                    }
                    System.out.println(atom);
                    t1.notify();
                }
            }
        }
    }

    public static void main(String[] args) {
        new Main();
    }
}

最佳答案

您需要成为“对象监视器的所有者”才能对其调用通知。到目前为止,您的方法都是 synchronized(this),但它们在其他对象(它们未同步)上调用 notify()。换句话说:

synchronized(t2) {
   t2.notify();
}

synchronized(t1) {
   t1.notify();
}

有关 java 中监视器和同步的完整说明,请参阅 here ,或者在 SO 上寻找类似的问题,比如这个 - Java Wait and Notify: IllegalMonitorStateException

关于java - IllegalMonitorStateException notify() 和 wait(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14234619/

相关文章:

c - 为接收函数设置套接字超时

java - 查找两个字符串中的共同字母(Java)

java - spring-hibernate3 与 hibernate-core-4.1.2

java - 这个 Java 程序是如何运行的?

java - 线程放弃等待并抛出异常的方法

c++ - QT:对 qApp 属性更改做一些事情

java - AbstractLine 不受支持的控件类型 : Master Gain Java

java - 在 Jena 自定义 PropertyFunction 中等待服务器响应

java - 如何暂停一个线程并在它停止的地方恢复它

java - notify() 和 notifyAll() 的区别