rzp0071 2017-07-06 07:11 采纳率: 0%
浏览 1263

关于 wait()/notify() 程序的异常问题

一段wait/notify的代码,有时运行成功,有时异常,请教

    package com.rzp.wxmp;
    public class TestThread {
    static volatile Integer sumSize = 0;
    static Integer currentSize = 0;

    public static void main(String[] args) {

        Producer p = new Producer();
        p.setDaemon(true);
        Customer c = new Customer();
        c.setDaemon(true);
        p.start();
        c.start();
        while (sumSize < 20) {

        }
    }


    static class Producer extends Thread {

        public Producer() {
            super("[Producer made ]");
        }

        @Override
        public void run() {
            made();
        }

        private void made() {
            while (sumSize < 20) {
                synchronized (currentSize) {
                    if (currentSize < 1) {
                        currentSize.notify();
                        System.out.println(Thread.currentThread().getName() + " made ,currentSize " + currentSize + " sumSize " + sumSize);
                        sumSize++;
                        currentSize++;
                    } else {
                        try {
                            currentSize.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            System.out.println(Thread.currentThread().getName() + "I`m made end ");
        }
    }


    static class Customer extends Thread {
        int eatSize = 0;


        public Customer() {
            super("[Customer  eat ]");
        }

        @Override
        public void run() {
            eat();
        }


        private void eat() {
            while (eatSize < 20) {
                synchronized (currentSize) {
                    if (currentSize > 0) {
                        currentSize.notify();
                        System.out.println(Thread.currentThread().getName() + " eat " + eatSize);
                        currentSize--;
                        eatSize++;
                    } else {
                        try {
                            currentSize.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            System.out.println(Thread.currentThread().getName() + "I`m eat end");
        }

    }
    } ```

异常信息

    ``` java
    [Producer made ] made ,currentSize 0 sumSize 0
    Exception in thread "[Customer  eat ]" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at com.rzp.wxmp.TestThread$Customer.eat(TestThread.java:75)
    at com.rzp.wxmp.TestThread$Customer.run(TestThread.java:67)
    ```
  • 写回答

2条回答

  • 夜空中的程序猿 2017-07-06 12:01
    关注

    因为你用currentSize作为锁对象 而你的currentSize在变啊 所以currentSize对应的锁肯定也变啊,所以会抛出该异常
    产生该异常的原因是不具备对象锁的情况下调用notify,notifyAll或者wait方法

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮