Home > Software design >  Can I implement BlockingQueue in Java using semaphores in this way?
Can I implement BlockingQueue in Java using semaphores in this way?

Time:03-23

Is this code good implementation of using synchronized mechanism using semaphore in Java, I'm not sure if the length variable is safe because we have 2 methods there, I think that this is the right way, but I need someone to confirm this for me. Thank you very much for your time!

public class BlockingQueue<T> {
    static int length;
    T[] contents;
    int capacity;

    public BlockingQueue(int capacity) {
        contents = (T[]) new Object[capacity];
        this.capacity = capacity;
    }

    public synchronized void enqueue(T item) throws InterruptedException  {
        while(length == this.capacity) {
            wait();
        }
        contents[length  ]= item;
        if(contents.length == 1) {
            notifyAll();
        }
    }

    public synchronized T dequeue() throws InterruptedException{
        while(length == 0){
            wait();
        }
        if(length == capacity){
            notifyAll();
        }
        T it = contents[0];
        for(int i=1;i<length;i  )
        {
            contents[i-1]=contents[i]; //shifting left
        }
        length--;
        return it;
    }
}

CodePudding user response:

The implementation looks correct. A few items can be improved I believe:

if(contents.length == 1) {
    notifyAll();
}

No need to wake all threads up, only one will win. notify() should suffice.

if(length == capacity){
   notifyAll();
}

Above, it makes sense to wake all threads up.

Instead of shifting contents, consider allocating capacity 1 and use a circular queue.

CodePudding user response:

Yes it is possible to do it this way, but i think you are reinventing the wheel. Give me a minute and i'll send a code sample...

EDIT: (see comments on this post) Well, you should have said that.

    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
    queue.add("hello");
    queue.add("queue");
    String removed = queue.remove();

But, as mentioned above, yes that would work. However, i understand the idea behind the idea behind the wait() method calls, but think its unneeded. If its full, throw an exception, same if its empty. That is, unless that is also part of the programming excersise.

  • Related