Question

Not a single operation of ArrayBlockingQueue is concurrent with any of its other operations; they always take the same lock. Even for the size() method it takes a lock.

 public int size() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return count;
        } finally {
            lock.unlock();
        }
    }

While for the implementation of LinkedBlockingQueue you have two locks: put and take. And for size() it uses AtomicInteger so doesn't need a lock.

So my question is: why is this implementation in the concurrent package - is ArrayBlockingQueue really concurrent?

Was it helpful?

Solution

ArrayBlockingQueue is in the java.util.concurrent package because multiple threads can use the object concurrently without thread-safety problems.

The ability to use multiple methods at the same time is not what the object is made for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top