Search code examples
javamultithreadingblockingqueue

Blocking queue not working in java, I can put more element than the defined size of blocking queue


I have declared a BlockingQueue of size 1

final BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(1);.

But I am able to add more than 1 element into the queue. I am sure I am missing something in this like this is the core property of BlockingQueue. Here is the code from java docs.

    /**
     * Creates an {@code ArrayBlockingQueue} with the given (fixed)
     * capacity and default access policy.
     *
     * @param capacity the capacity of this queue
     * @throws IllegalArgumentException if {@code capacity < 1}
     */
    public ArrayBlockingQueue(int capacity) {
        this(capacity, false);
    }

But when I am implementing I am able to produce more than 1 element, according to me if the size of the BlockingQueue is 1 then after producing 1 element it should wait for the consumer to consume the element. Here is the code and the output of the code.

I need an explanation for this, can you please help me with this.

I have tried google search and StackOverflow with no success.

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BlockingQueueTestWorking {

    public static void main(String[] args) {
        final BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(1);

        Runnable producer = () -> {
            try {
                int iterator = 0;
                while (true) {
                    if (iterator++ == 10)
                        break;

                    String name = Thread.currentThread().getName();
                    Integer i = (int) (Math.random() * 10);
                    blockingQueue.put(i);
                    System.out.println(name + " Producing:-> " + i);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        Runnable consumer = () -> {
            try {
                int iterator = 0;
                while (true) {
                    if (iterator++ == 10)
                        break;
                    String name = Thread.currentThread().getName();
                    Integer take = blockingQueue.take();
                    System.out.println(name + " Consuming:<- " + take);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        Thread threadProducer = new Thread(producer);


        final ExecutorService executor = Executors.newFixedThreadPool(10);
        executor.execute(threadProducer);
        
        Thread threadConsumer = new Thread(consumer);
        executor.execute(threadConsumer);

        executor.shutdown();
    }

}

Output:
pool-1-thread-1 Producing:-> 2
pool-1-thread-1 Producing:-> 7
pool-1-thread-2 Consuming:<- 2
pool-1-thread-2 Consuming:<- 7
pool-1-thread-1 Producing:-> 6
pool-1-thread-2 Consuming:<- 6
pool-1-thread-1 Producing:-> 2
pool-1-thread-2 Consuming:<- 2
pool-1-thread-1 Producing:-> 6
pool-1-thread-2 Consuming:<- 6
pool-1-thread-1 Producing:-> 1
pool-1-thread-2 Consuming:<- 1
pool-1-thread-1 Producing:-> 2
pool-1-thread-2 Consuming:<- 2
pool-1-thread-1 Producing:-> 2
pool-1-thread-2 Consuming:<- 2
pool-1-thread-1 Producing:-> 2
pool-1-thread-2 Consuming:<- 2
pool-1-thread-1 Producing:-> 4
pool-1-thread-2 Consuming:<- 4


Solution

  • Change the print to

    System.out.println(String.format("[%s]", new Timestamp(System.currentTimeMillis())) + " Producing:-> " + i);
    

    and

    System.out.println(String.format("[%s]", new Timestamp(System.currentTimeMillis())) + name + " Consuming:<- " + take);
    

    and you will see that they are actually not printed in the correct time order