This is a follow-up question to this question.
Is this a reasonable implementation for a blocking version of method ArrayBlockingQueue.drainTo(Collection)
?
/**
* This blocking method blends {@link #take()} with {@link #drainTo(Collection)}.
* <br>
* Chiefly, {@code take()} blocks and {@code drainTo(Collection)} does not block.
*/
public int drainAtLeastOneTo(Collection<? super E> c)
throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0) {
notEmpty.await();
}
final int x = drainTo(c);
return x;
}
finally {
lock.unlock();
}
}
To clarify: This code comes from the Sun JDK source code of ArrayBlockingQueue
. I simply blended the source from take()
with drainTo(Collection)
.
I am considering adding it to a modified version of ArrayBlockingQueue
.
notEmpty
? How much of your code is a rewrite ofArrayBlockingQueue
? Yourlock
lock is completely useless too, each time a thread calls the method they will get a new lock instance, so you have not implemented any locking at all..... your code does not work... though I suspect you did not know that when you posted it. – rolfl Aug 28 at 4:17lock
is a shadow-name of the instancelock
. That's a bad practice - using the same names in methods variables as instance variables... my first comment above is "wrong". – rolfl Aug 28 at 4:37