|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjava.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.concurrent.SynchronousQueue<E>
E - 此 collection 保持的元素类型public class SynchronousQueue<E>
一种阻塞队列,其中每个插入操作必须等待另一个线程的对应移除操作 ,反之亦然。同步队列没有任何内部容量,甚至连一个队列的容量都没有。不能在同步队列上进行 peek,因为仅在试图要移除元素时,该元素才存在;除非另一个线程试图移除某个元素,否则也不能(使用任何方法)插入元素;也不能迭代队列,因为其中没有元素可用于迭代。队列的头 是尝试添加到队列中的首个已排队插入线程的元素;如果没有这样的已排队线程,则没有可用于移除的元素并且 poll() 将会返回 null。对于其他 Collection 方法(例如 contains),SynchronousQueue 作为一个空 collection。此队列不允许 null 元素。
同步队列类似于 CSP 和 Ada 中使用的 rendezvous 信道。它非常适合于传递性设计,在这种设计中,在一个线程中运行的对象要将某些信息、事件或任务传递给在另一个线程中运行的对象,它就必须与该对象同步。
对于正在等待的生产者和使用者线程而言,此类支持可选的公平排序策略。默认情况下不保证这种排序。但是,使用公平设置为 true 所构造的队列可保证线程以 FIFO 的顺序进行访问。
此类及其迭代器实现 Collection 和 Iterator 接口的所有可选 方法。
此类是 Java Collections Framework 的成员。
| 构造方法摘要 | |
|---|---|
SynchronousQueue()
创建一个具有非公平访问策略的 SynchronousQueue。 |
|
SynchronousQueue(boolean fair)
创建一个具有指定公平策略的 SynchronousQueue。 |
|
| 方法摘要 | ||
|---|---|---|
void |
clear()
不执行任何操作。 |
|
boolean |
contains(Object o)
始终返回 false。 |
|
boolean |
containsAll(Collection<?> c)
除非给定 collection 为空,否则返回 false。 |
|
int |
drainTo(Collection<? super E> c)
移除此队列中所有可用的元素,并将它们添加到给定 collection 中。 |
|
int |
drainTo(Collection<? super E> c,
int maxElements)
最多从此队列中移除给定数量的可用元素,并将这些元素添加到给定 collection 中。 |
|
boolean |
isEmpty()
始终返回 true。 |
|
Iterator<E> |
iterator()
返回一个空迭代器,其中 hasNext 始终返回 false。 |
|
boolean |
offer(E e)
如果另一个线程正在等待以便接收指定元素,则将指定元素插入到此队列。 |
|
boolean |
offer(E o,
long timeout,
TimeUnit unit)
将指定元素插入到此队列,如有必要则等待指定的时间,以便另一个线程接收它。 |
|
E |
peek()
始终返回 null。 |
|
E |
poll()
如果另一个线程当前正要使用某个元素,则获取并移除此队列的头。 |
|
E |
poll(long timeout,
TimeUnit unit)
获取并移除此队列的头,如有必要则等待指定的时间,以便另一个线程插入它。 |
|
void |
put(E o)
将指定元素添加到此队列,如有必要则等待另一个线程接收它。 |
|
int |
remainingCapacity()
始终返回 0。 |
|
boolean |
remove(Object o)
始终返回 false。 |
|
boolean |
removeAll(Collection<?> c)
始终返回 false。 |
|
boolean |
retainAll(Collection<?> c)
始终返回 false。 |
|
int |
size()
始终返回 0。 |
|
E |
take()
获取并移除此队列的头,如有必要则等待另一个线程插入它。 |
|
Object[] |
toArray()
返回一个 0 长度的数组。 |
|
|
toArray(T[] a)
将指定数组的第 0 个元素设置为 null(如果该数组有非 0 的长度)并返回它。 |
|
| 从类 java.util.AbstractQueue 继承的方法 |
|---|
add, addAll, element, remove |
| 从类 java.util.AbstractCollection 继承的方法 |
|---|
toString |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 从接口 java.util.concurrent.BlockingQueue 继承的方法 |
|---|
add |
| 从接口 java.util.Queue 继承的方法 |
|---|
element, remove |
| 从接口 java.util.Collection 继承的方法 |
|---|
addAll, equals, hashCode |
| 构造方法详细信息 |
|---|
public SynchronousQueue()
public SynchronousQueue(boolean fair)
fair - 如果为 true,则等待线程以 FIFO 的顺序竞争访问;否则顺序是未指定的。| 方法详细信息 |
|---|
public void put(E o)
throws InterruptedException
BlockingQueue<E> 中的 puto - 要添加的元素
InterruptedException - 如果在等待时被中断
NullPointerException - 如果指定元素为 null
public boolean offer(E o,
long timeout,
TimeUnit unit)
throws InterruptedException
BlockingQueue<E> 中的 offero - 要添加的元素timeout - 放弃之前等待的时间长度,以 unit 为时间单位unit - 确定如何解释 timeout 参数的 TimeUnit
InterruptedException - 如果在等待时被中断
NullPointerException - 如果指定元素为 nullpublic boolean offer(E e)
BlockingQueue<E> 中的 offerQueue<E> 中的 offere - 要添加的元素
NullPointerException - 如果指定元素 null
public E take()
throws InterruptedException
BlockingQueue<E> 中的 takeInterruptedException - 如果在等待时被中断
public E poll(long timeout,
TimeUnit unit)
throws InterruptedException
BlockingQueue<E> 中的 polltimeout - 放弃之前要等待的时间长度,用 unit 的时间单位表示unit - 确定如何解释 timeout 参数的 TimeUnit
InterruptedException - 如果在等待时被中断public E poll()
Queue<E> 中的 pollpublic boolean isEmpty()
Collection<E> 中的 isEmptyAbstractCollection<E> 中的 isEmptypublic int size()
Collection<E> 中的 sizeAbstractCollection<E> 中的 sizepublic int remainingCapacity()
BlockingQueue<E> 中的 remainingCapacitypublic void clear()
Collection<E> 中的 clearAbstractQueue<E> 中的 clearpublic boolean contains(Object o)
Collection<E> 中的 containsBlockingQueue<E> 中的 containsAbstractCollection<E> 中的 containso - 元素
public boolean remove(Object o)
Collection<E> 中的 removeBlockingQueue<E> 中的 removeAbstractCollection<E> 中的 removeo - 要移除的元素
public boolean containsAll(Collection<?> c)
Collection<E> 中的 containsAllAbstractCollection<E> 中的 containsAllc - collection
AbstractCollection.contains(Object)public boolean removeAll(Collection<?> c)
Collection<E> 中的 removeAllAbstractCollection<E> 中的 removeAllc - collection
AbstractCollection.remove(Object),
AbstractCollection.contains(Object)public boolean retainAll(Collection<?> c)
Collection<E> 中的 retainAllAbstractCollection<E> 中的 retainAllc - collection
AbstractCollection.remove(Object),
AbstractCollection.contains(Object)public E peek()
Queue<E> 中的 peekpublic Iterator<E> iterator()
Iterable<E> 中的 iteratorCollection<E> 中的 iteratorAbstractCollection<E> 中的 iteratorpublic Object[] toArray()
Collection<E> 中的 toArrayAbstractCollection<E> 中的 toArraypublic <T> T[] toArray(T[] a)
Collection<E> 中的 toArrayAbstractCollection<E> 中的 toArraya - 数组
NullPointerException - 如果指定的数组为 nullpublic int drainTo(Collection<? super E> c)
BlockingQueue 复制的描述
BlockingQueue<E> 中的 drainToc - 接收传输元素的 collection
UnsupportedOperationException - 如果指定 collection 不支持添加元素
ClassCastException - 如果此队列元素的类不允许将其添加到指定 collection
NullPointerException - 如果指定 collection 为 null
IllegalArgumentException - 如果指定 collection 是此队列,或者此队列元素的某些属性不允许将其添加到指定 collection
public int drainTo(Collection<? super E> c,
int maxElements)
BlockingQueue 复制的描述
BlockingQueue<E> 中的 drainToc - 接收传输元素的 collectionmaxElements - 传输元素的最大数量
UnsupportedOperationException - 如果指定 collection 不支持添加元素
ClassCastException - 如果此队列元素的类不允许将其添加到指定 collection
NullPointerException - 如果指定 collection 为 null
IllegalArgumentException - 如果指定 collection 是此队列,或者此队列元素的某些属性不允许将其添加到指定 collection
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。