Fields Summary |
---|
private static final long | serialVersionUID |
static final int | NCPUSThe number of CPUs, for spin control |
static final int | maxTimedSpinsThe number of times to spin before blocking in timed waits.
The value is empirically derived -- it works well across a
variety of processors and OSes. Empirically, the best value
seems not to vary with number of CPUs (beyond 2) so is just
a constant. |
static final int | maxUntimedSpinsThe number of times to spin before blocking in untimed waits.
This is greater than timed value because untimed waits spin
faster since they don't need to check times on each spin. |
static final long | spinForTimeoutThresholdThe number of nanoseconds for which it is faster to spin
rather than to use timed park. A rough estimate suffices. |
private volatile transient Transferer | transfererThe transferer. Set only in constructor, but cannot be declared
as final without further complicating serialization. Since
this is accessed only at most once per public method, there
isn't a noticeable performance penalty for using volatile
instead of final here. |
private ReentrantLock | qlock |
private WaitQueue | waitingProducers |
private WaitQueue | waitingConsumers |
Methods Summary |
---|
public void | clear()Does nothing.
A SynchronousQueue has no internal capacity.
|
public boolean | contains(java.lang.Object o)Always returns false.
A SynchronousQueue has no internal capacity.
return false;
|
public boolean | containsAll(java.util.Collection c)Returns false unless the given collection is empty.
A SynchronousQueue has no internal capacity.
return c.isEmpty();
|
public int | drainTo(java.util.Collection c)
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
int n = 0;
E e;
while ( (e = poll()) != null) {
c.add(e);
++n;
}
return n;
|
public int | drainTo(java.util.Collection c, int maxElements)
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
int n = 0;
E e;
while (n < maxElements && (e = poll()) != null) {
c.add(e);
++n;
}
return n;
|
public boolean | isEmpty()Always returns true.
A SynchronousQueue has no internal capacity.
return true;
|
public java.util.Iterator | iterator()Returns an empty iterator in which hasNext always returns
false.
return new EmptyIterator<E>();
|
public boolean | offer(E o, long timeout, java.util.concurrent.TimeUnit unit)Inserts the specified element into this queue, waiting if necessary
up to the specified wait time for another thread to receive it.
if (o == null) throw new NullPointerException();
if (transferer.transfer(o, true, unit.toNanos(timeout)) != null)
return true;
if (!Thread.interrupted())
return false;
throw new InterruptedException();
|
public boolean | offer(E e)Inserts the specified element into this queue, if another thread is
waiting to receive it.
if (e == null) throw new NullPointerException();
return transferer.transfer(e, true, 0) != null;
|
public E | peek()Always returns null.
A SynchronousQueue does not return elements
unless actively waited on.
return null;
|
public E | poll(long timeout, java.util.concurrent.TimeUnit unit)Retrieves and removes the head of this queue, waiting
if necessary up to the specified wait time, for another thread
to insert it.
Object e = transferer.transfer(null, true, unit.toNanos(timeout));
if (e != null || !Thread.interrupted())
return (E)e;
throw new InterruptedException();
|
public E | poll()Retrieves and removes the head of this queue, if another thread
is currently making an element available.
return (E)transferer.transfer(null, true, 0);
|
public void | put(E o)Adds the specified element to this queue, waiting if necessary for
another thread to receive it.
if (o == null) throw new NullPointerException();
if (transferer.transfer(o, false, 0) == null) {
Thread.interrupted();
throw new InterruptedException();
}
|
private void | readObject(java.io.ObjectInputStream s)
s.defaultReadObject();
if (waitingProducers instanceof FifoWaitQueue)
transferer = new TransferQueue();
else
transferer = new TransferStack();
|
public int | remainingCapacity()Always returns zero.
A SynchronousQueue has no internal capacity.
return 0;
|
public boolean | remove(java.lang.Object o)Always returns false.
A SynchronousQueue has no internal capacity.
return false;
|
public boolean | removeAll(java.util.Collection c)Always returns false.
A SynchronousQueue has no internal capacity.
return false;
|
public boolean | retainAll(java.util.Collection c)Always returns false.
A SynchronousQueue has no internal capacity.
return false;
|
public int | size()Always returns zero.
A SynchronousQueue has no internal capacity.
return 0;
|
public E | take()Retrieves and removes the head of this queue, waiting if necessary
for another thread to insert it.
Object e = transferer.transfer(null, false, 0);
if (e != null)
return (E)e;
Thread.interrupted();
throw new InterruptedException();
|
public java.lang.Object[] | toArray()Returns a zero-length array.
return new Object[0];
|
public T[] | toArray(T[] a)Sets the zeroeth element of the specified array to null
(if the array has non-zero length) and returns it.
if (a.length > 0)
a[0] = null;
return a;
|
private void | writeObject(java.io.ObjectOutputStream s)Save the state to a stream (that is, serialize it).
boolean fair = transferer instanceof TransferQueue;
if (fair) {
qlock = new ReentrantLock(true);
waitingProducers = new FifoWaitQueue();
waitingConsumers = new FifoWaitQueue();
}
else {
qlock = new ReentrantLock();
waitingProducers = new LifoWaitQueue();
waitingConsumers = new LifoWaitQueue();
}
s.defaultWriteObject();
|