Method from EDU.oswego.cs.dl.util.concurrent.LinkedQueue Detail: |
protected synchronized Object extract() {
synchronized(head_) {
Object x = null;
LinkedNode first = head_.next;
if (first != null) {
x = first.value;
first.value = null;
head_ = first;
}
return x;
}
}
Main mechanics for take/poll * |
protected void insert(Object x) {
synchronized(putLock_) {
LinkedNode p = new LinkedNode(x);
synchronized(last_) {
last_.next = p;
last_ = p;
}
if (waitingForTake_ > 0)
putLock_.notify();
}
}
Main mechanics for put/offer * |
public boolean isEmpty() {
synchronized(head_) {
return head_.next == null;
}
}
|
public boolean offer(Object x,
long msecs) throws InterruptedException {
if (x == null) throw new IllegalArgumentException();
if (Thread.interrupted()) throw new InterruptedException();
insert(x);
return true;
}
|
public Object peek() {
synchronized(head_) {
LinkedNode first = head_.next;
if (first != null)
return first.value;
else
return null;
}
}
|
public Object poll(long msecs) throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
Object x = extract();
if (x != null)
return x;
else {
synchronized(putLock_) {
try {
long waitTime = msecs;
long start = (msecs < = 0)? 0 : System.currentTimeMillis();
++waitingForTake_;
for (;;) {
x = extract();
if (x != null || waitTime < = 0) {
--waitingForTake_;
return x;
}
else {
putLock_.wait(waitTime);
waitTime = msecs - (System.currentTimeMillis() - start);
}
}
}
catch(InterruptedException ex) {
--waitingForTake_;
putLock_.notify();
throw ex;
}
}
}
}
|
public void put(Object x) throws InterruptedException {
if (x == null) throw new IllegalArgumentException();
if (Thread.interrupted()) throw new InterruptedException();
insert(x);
}
|
public Object take() throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
// try to extract. If fail, then enter wait-based retry loop
Object x = extract();
if (x != null)
return x;
else {
synchronized(putLock_) {
try {
++waitingForTake_;
for (;;) {
x = extract();
if (x != null) {
--waitingForTake_;
return x;
}
else {
putLock_.wait();
}
}
}
catch(InterruptedException ex) {
--waitingForTake_;
putLock_.notify();
throw ex;
}
}
}
}
|