首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >等待notify()和IllegalMonitorStateException ()

等待notify()和IllegalMonitorStateException ()
EN

Stack Overflow用户
提问于 2013-01-09 19:52:25
回答 1查看 19.8K关注 0票数 9

我有个问题。当我在synchronized块中使用notify()时,我得到了IllegalMonitorStateException。有人能帮我解决这个问题吗?

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

Main.java:

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-09 19:58:55

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

代码语言:javascript
复制
synchronized(t2) {
   t2.notify();
}

代码语言:javascript
复制
synchronized(t1) {
   t1.notify();
}

有关java中的监视器和同步的完整解释,请参阅here,或者在SO上查找类似的问题,就像这个问题- Java Wait and Notify: IllegalMonitorStateException

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14234619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档