Exception in thread "notify01" java.lang.IllegalMonitorStateException

teddy9393 2014-07-25 03:16:36
package com.xiaonei2shou.test;

public class ThreadTest {
private String flag = "true";

class NotifyThread extends Thread {
public NotifyThread(String name) {
super(name);
}

public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (flag) {
flag = "false";
flag.notifyAll();
}
}
};

class WaitThread extends Thread {
public WaitThread(String name) {
super(name);
}

public void run() {
synchronized (flag) {
while (flag != "false") {
long waitTime = System.currentTimeMillis();
System.out.println(getName() + " begin waiting!");
try {
flag.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
waitTime = System.currentTimeMillis() - waitTime;
System.out.println(getName() + " wait time :" + waitTime);
try {
sleep(1000);
System.out.println(getName() + " sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(getName() + " end waiting!");
}
}
}

public static void main(String[] args) throws InterruptedException {
System.out.println("Main Thread Run!");
ThreadTest test = new ThreadTest();
NotifyThread notifyThread = test.new NotifyThread("notify01");
WaitThread waitThread01 = test.new WaitThread("waiter01");
WaitThread waitThread02 = test.new WaitThread("waiter02");
WaitThread waitThread03 = test.new WaitThread("waiter03");
waitThread02.start();
waitThread01.start();
waitThread03.start();
notifyThread.start();
}

}
Exception in thread "notify01" java.lang.IllegalMonitorStateException 报错了 啥原因
...全文
594 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
oh_Maxy 版主 2014-07-25
  • 打赏
  • 举报
回复
关键问题:测wait和notify的时候,必须保证是同一个对象。 次要问题:字符串比较要使用equals方法。
oh_Maxy 版主 2014-07-25
  • 打赏
  • 举报
回复
给你把代码调了下,变动的地方加了注释:

public class ThreadTest {
    private String flag = "true";
    private Object locker = new Object();

    class NotifyThread extends Thread {
        public NotifyThread(String name) {
            super(name);
        }

        public void run() {
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 使用专门的Object对象作为测试wait和notify的锁
            //因为当flag从"true"变成"false"时,flag指向的对象已经变了
            synchronized (locker) {
                flag = "false";
                locker.notifyAll();
            }
        }
    };

    class WaitThread extends Thread {
        public WaitThread(String name) {
            super(name);
        }

        public void run() {
            // wait和nonify都用一个Object对象实现
            synchronized (locker) {
                // 字符串不能用=或者!=比较
                while (!flag .equals("false")) {
                    long waitTime = System.currentTimeMillis();
                    System.out.println(getName() + " begin waiting!");
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    waitTime = System.currentTimeMillis() - waitTime;
                    System.out.println(getName() + " wait time :" + waitTime);
                    try {
                        sleep(1000);
                        System.out.println(getName() + " sleep");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(getName() + " end waiting!");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Main Thread Run!");
        ThreadTest test = new ThreadTest();
        NotifyThread notifyThread = test.new NotifyThread("notify01");
        WaitThread waitThread01 = test.new WaitThread("waiter01");
        WaitThread waitThread02 = test.new WaitThread("waiter02");
        WaitThread waitThread03 = test.new WaitThread("waiter03");
        waitThread02.start();
        waitThread01.start();
        waitThread03.start();
        notifyThread.start();
    }

}
姜小白- 2014-07-25
  • 打赏
  • 举报
回复
抱歉 看错代码了 ThreadTest test = new ThreadTest(); NotifyThread notifyThread = test.new NotifyThread("notify01"); 以为你这个test 是带的部分包名呢,就说怎么有这种写法呢 原来这个test是用来调用内部类的
姜小白- 2014-07-25
  • 打赏
  • 举报
回复
NotifyThread notifyThread = test.new NotifyThread("notify01"); 去掉前面的test.

50,541

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧