IllegalMonitorStateException notify()和wait()

时间:2013-01-09 11:52:25

标签: java wait notify illegalmonitorstateexcep

我有问题。当我在synchronized块中使用notify()时,我有IllegalMonitorStateException。任何人都可以帮我解决这个问题吗?

我必须这样做,一个线程将发送到第二个线程char,然后这个线程必须等待,第二个线程打印此char。在第二个线程等待之后,第一个再次发送下一个char

Main.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();
    }
}

1 个答案:

答案 0 :(得分:9)

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

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

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

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