What is the use of the AtomicInteger internal lock

In this section, from the java.util.concurrent.LinkedBlockingQueue.put () method, I think AtomicInteger is not needed. Given that inside lock () there is only one thread. Am I right?

putLock.lockInterruptibly();
    try {
        while (count.get() == capacity) {
            notFull.await();
        }
        enqueue(node);
        c = count.getAndIncrement();
        if (c + 1 < capacity)
            notFull.signal();
    } finally {
        putLock.unlock();
    }

I checked a lot of questions but cannot find the answer. My question is: there is only one thread inside the castle, so why use AtomicInteger here.

+4
source share
2 answers

LinkedBlockingQueue putLock takeLock, put take, , , , ( ).

, , count . , , count, .

, , size() remainingCapacity(), count . , , peek() poll() ( -), , count , null , .

+7

:

private final ReentrantLock putLock = new ReentrantLock();

private final ReentrantLock takeLock = new ReentrantLock();

put putLock, , count, ( put). , , Atomic.

+5

Source: https://habr.com/ru/post/1681356/


All Articles