Threads synchronizing in Java, IllegalMonitorStateException

左心房为你撑大大i 提交于 2019-12-13 07:04:51

问题


I am trying to synchronize two threads - the "Main" thread, and a runnable. I get the IllegalMonitorStateException, but I do not completelty understand what "you do not have the lock of the object" means.

Here is my code:

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:


You may call wait() method only on objects you synchronizing on.
So, since you have synchronized (ThreadsTest.getInstance()), you must write ThreadsTest.getInstance().wait().

Not sure what you trying to test here exactly, if it's just basic thread sync sample, then you should change your code to

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


来源:https://stackoverflow.com/questions/3773807/threads-synchronizing-in-java-illegalmonitorstateexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!