Home  >  Q&A  >  body text

java - Some doubts about notify()/wait()

class MyObject{
    private Queue<String> queue = new ConcurrentLinkedQueue<String>();
    public synchronized void set(String s){
            while(queue.size() >= 10){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            queue.add(s);
            notify();
    }
}

class Producer implements Runnable{
    private MyObject myObj;
    
    public Producer(MyObject myObj) {
        this.myObj= myObj;
    }

    @Override
    public void run() {
        // 每条线程执行30次set
        for (int i = 0; i < 30; i++) {
            this.myObj.set("obj:" + i);
        }
    }
}

public static void main(String[] args){
    Producer producer = new Producer(new MyObject());
        
    // 生成30条线程
    for (int i = 0; i < 10; i++) {
        Thread thread = new Thread(producer);
        thread.start();
    }
    // 运行结果是只set了30次
}

My doubt is that when notify() publishes a notification, why does it not let the wait() method of other threads continue to execute?

PHP中文网PHP中文网2736 days ago693

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-05-17 10:00:07

    When the number of your queues is greater than 10, each of your threads will start wait()住了, 不会走到notify()的啊. 你需要一个单独的线程去监控队列的大小, 大于10的时候notify() first. For example, you can change yours slightly

    class MyObject {
        private Queue<String> queue = new ConcurrentLinkedQueue<String>();
    
        private volatile int limit = 10;
    
        public synchronized void set(String s) {
          if (queue.size() >= limit) {
            try {
              wait();
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
          queue.add(s);
        }
    
        public synchronized void delta() {
          if (queue.size() >= limit) {
            limit += 10;
            notify();
          }
        }
    }

    Then there is a monitoring thread

    class Monitor implements Runnable {
    
        private MyObject myObj;
    
        public Monitor(MyObject myObj) {
          this.myObj = myObj;
        }
    
        @Override
        public void run() {
          while (true) {
            myObj.delta();
          }
        }
    }

    reply
    0
  • Cancelreply