Java 中的线程同步,IllegalMonitorStateException

发布于 2024-09-24 05:33:33 字数 1119 浏览 2 评论 0原文

我正在尝试同步两个线程 - “主”线程和一个可运行线程。我收到 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();

    }
}

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏见 2024-10-01 05:33:33

您只能在同步的对象上调用 wait() 方法。
因此,由于您已同步 (ThreadsT​​est.getInstance()),因此必须编写 ThreadsT​​est.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();
    }
}

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文