线程在Java中同步,IllegalMonitorStateException

时间:2010-09-22 21:34:02

标签: multithreading synchronizing illegalmonitorstateexcep

我正在尝试同步两个线程 - “主”线程和一个可运行的线程。我得到IllegalMonitorStateException,但我不完全理解“你没有对象的锁”的意思。

这是我的代码:

public class ThreadsTest {
    private static ThreadsTest instance;
    public volatile boolean flag = false;

    public void doStuff() {
        System.out.println("first");

        this.flag = true;

    }

    public Runnable mDrawer = new Runnable() {

        public void run() {
            synchronized (ThreadsTest.getInstance()) {
                while (flag == false)
                    try {
                        wait();
                    } catch (InterruptedException ie) {
                        System.out.println("second");
                    }
            }



        }
    };

    public static ThreadsTest getInstance() {
        if (ThreadsTest.instance == null) {
            ThreadsTest.instance = new ThreadsTest();
        }
        return ThreadsTest.instance;
    }

    private ThreadsTest() {
    }

    public static void main(String[] args) {
        ThreadsTest t = ThreadsTest.getInstance();
        t.mDrawer.run();
        t.doStuff();

    }
}

1 个答案:

答案 0 :(得分:2)

您只能对同步的对象调用wait()方法 因此,由于您有synchronized (ThreadsTest.getInstance()),因此您必须撰写ThreadsTest.getInstance().wait()

不确定您要在此处测试的是什么,如果它只是基本的线程同步示例,那么您应该将代码更改为

public class ThreadsTest {
    private static ThreadsTest instance;
    public volatile boolean flag = false;

    public void doStuff() {
        System.out.println("first");
        this.flag = true;
        synchronized (getInstance()) {
            getInstance().notifyAll();
        }
    }

    public Runnable mDrawer = new Runnable() {

        public void run() {
            synchronized (ThreadsTest.getInstance()) {
                while (flag == false)
                    try {
                        ThreadsTest.getInstance().wait();
                    } catch (InterruptedException ie) {
                        System.out.println("second");
                    }
            }



        }
    };

    public static ThreadsTest getInstance() {
        if (ThreadsTest.instance == null) {
            ThreadsTest.instance = new ThreadsTest();
        }
        return ThreadsTest.instance;
    }

    private ThreadsTest() {
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadsTest t = ThreadsTest.getInstance();
        new Thread(t.mDrawer).start();
        Thread.sleep(1000L); // let other thread start, so it won't skip our notify()
        t.doStuff();
    }
}