In my implementation I use DelayQueue
to prevent too many concurrent access of my method. I found that DelayQueue
use internal lock
on both methods poll
and offer
, but in my case I need to do something like this:
if(Objects.nonNull(delayQueue.poll())) {
delayQueue.offer(...);
}
And those few lines of code are not atomic right? I can add my own ReentrantLock
and do something like:
try {
lock.lock();
if(poll()) {
offer(...)
}
finally {
lock.unlock();
}
But it looks redundant for me as we have now 2 internal calls to lock/unlock
and one external.
Is there a better way to do it?