Java BlockingQueue confusion

2020-08-07T17:56:09

I am currently reading about Java BlockingQueue and this example is given on many websites for a simple implementation of the BlockingQueue. The code is simple but I am a bit confused. For example lets say we fill up the queue, then we try to enqueue 3 more times. This will make the 3 threads wait. Then when we call dequeue, the code in the dequeue method will enter the second if statement and it will notify all threads. Won't this mean that the 3 threads waiting will all add nodes to the queue? That means we will have 2 more nodes than the limit? I am pretty sure I misunderstood something here so I could use some small explanation.

public class BlockingQueue {

  private List queue = new LinkedList();
  private int  limit = 10;

  public BlockingQueue(int limit){
    this.limit = limit;
  }


  public synchronized void enqueue(Object item)
  throws InterruptedException  {
    while(this.queue.size() == this.limit) {
      wait();
    }
    this.queue.add(item);
    if(this.queue.size() == 1) {
      notifyAll();
    }
  }


  public synchronized Object dequeue()
  throws InterruptedException{
    while(this.queue.size() == 0){
      wait();
    }
    if(this.queue.size() == this.limit){
      notifyAll();
    }

    return this.queue.remove(0);
  }

}

Copyright License:
Author:「user737163」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/63299412/java-blockingqueue-confusion

About “Java BlockingQueue confusion” questions

I am currently reading about Java BlockingQueue and this example is given on many websites for a simple implementation of the BlockingQueue. The code is simple but I am a bit confused. For example ...
Does anybody know why java's BlockingQueue does not have a putAll method? Is there a problem with such a method? Any good ways around this problem without having to completely re-implement Blocking...
In my Java Application I have BlockingQueue<HashMap<Integer, double[]>> q How do I clone it?
I know that concurrent adds to an stl queue in c++ can cause issues, and the way to solve this is adding a mutex lock around all add/remove calls. But I am programming in Java at the moment, and ...
This is the code snippt of my input spout for emmiting tuple to a processing noded for stream processing over a cluster. The problem is The BlockingQueue is throwing InterruptedException . private
I have a Java program that sent messages to an external Rest API. This API can handle a payload with a max size of 500KB only. My messages are stored in a BlockingQueue. I was wondering if there i...
I trying to write some blob from database to disk by passing an InputStream object to the queue. It creates the file with size 0. When I have tried write the InputStream to the disk without using a
I'd like to have one BlockingMap data structure which is very similar to BlockingQueue. The take method of BlockingQueue will wait there until element is available. I'd like the get method of Block...
I'd like to have one BlockingMap data structure which is very similar to BlockingQueue. The take method of BlockingQueue will wait there until element is available. I'd like the get method of Block...
I was trying to make a java program to copy a file with the reader in one thread and the writer in another thread. The program works without showing any errors but the copied file has a different

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.