Last active
January 1, 2016 02:29
-
-
Save 73ddy/8079829 to your computer and use it in GitHub Desktop.
LinkedBlockingQueue#take method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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