Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed May 29, 2015
1 parent f66a4c2 commit e2a1d75
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/main/java/lbmq/LinkedBlockingMultiQueue.java
Expand Up @@ -600,6 +600,11 @@ public void put(E e) throws InterruptedException {
if (e == null)
throw new NullPointerException();
long oldSize = -1;
/*
* As this method never fails to insert, it is more efficient to pre-create the node outside the lock, to
* reduce contention
*/
Node<E> node = new Node<E>(e);
putLock.lockInterruptibly();
try {
/*
Expand All @@ -611,7 +616,7 @@ public void put(E e) throws InterruptedException {
while (count.get() == capacity) {
notFull.await();
}
enqueue(new Node<E>(e));
enqueue(node);
if (count.getAndIncrement() + 1 < capacity) {
// queue not full after adding, notify next offerer
notFull.signal();
Expand Down Expand Up @@ -661,6 +666,8 @@ public boolean offer(E e) {
if (e == null)
throw new NullPointerException();
long oldSize = -1;
if (count.get() == capacity)
return false;
putLock.lock();
try {
if (count.get() == capacity)
Expand Down Expand Up @@ -838,10 +845,9 @@ public Iterator<E> iterator() {
}

private class Itr implements Iterator<E> {
/*
* Basic weakly-consistent iterator. At all times hold the next
* item to hand out so that if hasNext() reports true, we will
* still have it to return even if lost race with a take, etc.
/**
* Basic weakly-consistent iterator. At all times hold the next item to hand out so that if hasNext()
* reports true, we will still have it to return even if lost race with a take, etc.
*/

private Node<E> current;
Expand Down

0 comments on commit e2a1d75

Please sign in to comment.