Use non-reentrant lock for GrowableArrayBlockingQueue tail
@@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
+import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer;
/**
@@ -42,7 +43,7 @@ public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements B
private final ReentrantLock headLock = new ReentrantLock();
private final PaddedInt headIndex = new PaddedInt();
private final PaddedInt tailIndex = new PaddedInt();
- private final ReentrantLock tailLock = new ReentrantLock();
+ private final StampedLock tailLock = new StampedLock();
private final Condition isNotEmpty = headLock.newCondition();
private T[] data;
@@ -126,7 +127,7 @@ public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements B
@Override
public void put(T e) {
- tailLock.lock();
+ long stamp = tailLock.writeLock();
boolean wasEmpty = false;
@@ -141,7 +142,7 @@ public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements B
wasEmpty = true;
}
} finally {
- tailLock.unlock();
+ tailLock.unlockWrite(stamp);
}
if (wasEmpty) {
@@ -278,7 +279,7 @@ public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements B
@Override
public boolean remove(Object o) {
- tailLock.lock();
+ long stamp = tailLock.writeLock();
headLock.lock();
try {
@@ -297,7 +298,7 @@ public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements B
}
} finally {
headLock.unlock();
- tailLock.unlock();
+ tailLock.unlockWrite(stamp);
}
return false;
@@ -347,7 +348,7 @@ public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements B
@Override
public void forEach(Consumer<? super T> action) {
- tailLock.lock();
+ long stamp = tailLock.writeLock();
headLock.lock();
try {
@@ -364,7 +365,7 @@ public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements B
} finally {
headLock.unlock();
- tailLock.unlock();
+ tailLock.unlockWrite(stamp);
}
}
@@ -372,7 +373,7 @@ public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements B
public String toString() {
StringBuilder sb = new StringBuilder();
- tailLock.lock();
+ long stamp = tailLock.writeLock();
headLock.lock();
try {
@@ -395,7 +396,7 @@ public class GrowableArrayBlockingQueue<T> extends AbstractQueue<T> implements B
sb.append(']');
} finally {
headLock.unlock();
- tailLock.unlock();
+ tailLock.unlockWrite(stamp);
}
return sb.toString();
}
