Skip to content

Commit a1b33a9

Browse files
committed
Reduce lock contention in concurrent queue poll and peek
1 parent 1506488 commit a1b33a9

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

core/src/main/java/org/mini2Dx/miniscript/core/util/AbstractConcurrentQueue.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ public E remove() {
157157

158158
@Override
159159
public E poll() {
160+
lock.lockRead();
161+
if(internalQueue.isEmpty()) {
162+
lock.unlockRead();
163+
return null;
164+
}
165+
lock.unlockRead();
166+
160167
lock.lockWrite();
161168
try {
162169
return internalQueue.poll();
@@ -177,6 +184,13 @@ public E element() {
177184

178185
@Override
179186
public E peek() {
187+
lock.lockRead();
188+
if(internalQueue.isEmpty()) {
189+
lock.unlockRead();
190+
return null;
191+
}
192+
lock.unlockRead();
193+
180194
lock.lockRead();
181195
try {
182196
return internalQueue.peek();

0 commit comments

Comments
 (0)