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.
LinkedBlockingQueue putLock takeLock, put take, , , , ( ).
LinkedBlockingQueue
putLock
takeLock
put
take
, , count . , , count, .
count
, , size() remainingCapacity(), count . , , peek() poll() ( -), , count , null , .
size()
remainingCapacity()
peek()
poll()
null
:
private final ReentrantLock putLock = new ReentrantLock(); private final ReentrantLock takeLock = new ReentrantLock();
put putLock, , count, ( put). , , Atomic.
Atomic
Source: https://habr.com/ru/post/1681356/More articles:collect2.exe: error: ld returned 5 exit status - c ++How to use (?! ...) a regular expression pattern to skip the whole unsurpassed part? - c #Is it possible to add static styles. Css enable via HtmlWebpackPlugin? - webpackCan I run two threads with my own Tensorflow session within the same python process? - multithreadingPrepare a form field response in Angular 2 - angularI got an AWS CodeBuild error. I have no clue to solve this - amazon-web-services"FATAL: Unable to load configuration" after chef-client - chefInverse words in a string except punctuation - c #Make text outside a div invisible - htmlSend signature with Ionic 3 signature - typescriptAll Articles