相关疑难解决方法(0)

在Integer上同步时对notify()的IllegalMonitorStateException

我刚接触在Java中使用wait()和notify(),我得到一个IllegalMonitorStateException.

主要代码

public class ThreadTest {

    private static Integer state = 0;
    public static void main(String[] args) {

        synchronized(state) {
            System.out.println("Starting thread");

            Thread t = new Thread(new AnotherTest());
            t.start();

            synchronized(state) {
                state = 0;
                while(state == 0) {
                    try {
                        state.wait(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                System.out.println("State is: " + state);
            }
        }
    }   

    public static class AnotherTest implements Runnable {

        @Override
        public void run() {
            synchronized(state) {
                state = 1;
                state.notify(); …
Run Code Online (Sandbox Code Playgroud)

java illegalmonitorstateexcep notify wait

1
推荐指数
1
解决办法
5061
查看次数

标签 统计

illegalmonitorstateexcep ×1

java ×1

notify ×1

wait ×1