veritas
veritas

Reputation: 2444

ArrayBlockingQueue - is it really Concurrent?

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?

Upvotes: 2

Views: 987

Answers (1)

Duncan Jones
Duncan Jones

Reputation: 69329

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.

Upvotes: 8

Related Questions