Skip to content

Instantly share code, notes, and snippets.

@73ddy
Last active January 1, 2016 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 73ddy/8079829 to your computer and use it in GitHub Desktop.
Save 73ddy/8079829 to your computer and use it in GitHub Desktop.
LinkedBlockingQueue#take method
public E take() throws InterruptedException {
E x;
int c = -1;
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
try {
while (count.get() == 0)
notEmpty.await();
} catch (InterruptedException ie) {
notEmpty.signal(); // propagate to a non-interrupted thread
throw ie;
}
x = extract();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment