ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TransferQueue.java
Revision: 1.3
Committed: Fri Jul 31 20:41:13 2009 UTC (14 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +15 -12 lines
Log Message:
sync with jsr166 package

File Contents

# User Rev Content
1 jsr166 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5     */
6    
7     package java.util.concurrent;
8    
9     /**
10     * A {@link BlockingQueue} in which producers may wait for consumers
11     * to receive elements. A {@code TransferQueue} may be useful for
12     * example in message passing applications in which producers
13 jsr166 1.2 * sometimes (using method {@link #transfer}) await receipt of
14     * elements by consumers invoking {@code take} or {@code poll}, while
15     * at other times enqueue elements (via method {@code put}) without
16     * waiting for receipt. {@linkplain
17     * #tryTransfer(Object) Non-blocking} and {@linkplain
18     * #tryTransfer(Object,long,TimeUnit) time-out} versions of {@code
19     * tryTransfer} are also available. A {@code TransferQueue} may also
20     * be queried, via {@link #hasWaitingConsumer}, whether there are any
21     * threads waiting for items, which is a converse analogy to a {@code
22     * peek} operation.
23 jsr166 1.1 *
24 jsr166 1.2 * <p>Like other blocking queues, a {@code TransferQueue} may be
25 jsr166 1.1 * capacity bounded. If so, an attempted {@code transfer} operation
26     * may initially block waiting for available space, and/or
27     * subsequently block waiting for reception by a consumer. Note that
28     * in a queue with zero capacity, such as {@link SynchronousQueue},
29     * {@code put} and {@code transfer} are effectively synonymous.
30     *
31     * <p>This interface is a member of the
32     * <a href="{@docRoot}/../technotes/guides/collections/index.html">
33     * Java Collections Framework</a>.
34     *
35     * @since 1.7
36     * @author Doug Lea
37     * @param <E> the type of elements held in this collection
38     */
39     public interface TransferQueue<E> extends BlockingQueue<E> {
40     /**
41 jsr166 1.3 * Transfers the specified element immediately if there exists a
42     * consumer already waiting to receive it (in {@link #take} or
43     * timed {@link #poll(long,TimeUnit) poll}), otherwise returning
44     * {@code false} without enqueuing the element.
45 jsr166 1.1 *
46     * @param e the element to transfer
47     * @return {@code true} if the element was transferred, else
48     * {@code false}
49     * @throws ClassCastException if the class of the specified element
50     * prevents it from being added to this queue
51     * @throws NullPointerException if the specified element is null
52     * @throws IllegalArgumentException if some property of the specified
53     * element prevents it from being added to this queue
54     */
55     boolean tryTransfer(E e);
56    
57     /**
58     * Inserts the specified element into this queue, waiting if
59     * necessary for space to become available and the element to be
60 jsr166 1.3 * received by a consumer invoking {@code take} or {@code poll}.
61 jsr166 1.1 *
62     * @param e the element to transfer
63     * @throws InterruptedException if interrupted while waiting,
64     * in which case the element is not enqueued
65     * @throws ClassCastException if the class of the specified element
66     * prevents it from being added to this queue
67     * @throws NullPointerException if the specified element is null
68     * @throws IllegalArgumentException if some property of the specified
69     * element prevents it from being added to this queue
70     */
71     void transfer(E e) throws InterruptedException;
72    
73     /**
74     * Inserts the specified element into this queue, waiting up to
75     * the specified wait time if necessary for space to become
76 jsr166 1.3 * available and the element to be received by a consumer invoking
77 jsr166 1.1 * {@code take} or {@code poll}.
78     *
79     * @param e the element to transfer
80     * @param timeout how long to wait before giving up, in units of
81     * {@code unit}
82     * @param unit a {@code TimeUnit} determining how to interpret the
83     * {@code timeout} parameter
84     * @return {@code true} if successful, or {@code false} if
85     * the specified waiting time elapses before completion,
86     * in which case the element is not enqueued
87     * @throws InterruptedException if interrupted while waiting,
88     * in which case the element is not enqueued
89     * @throws ClassCastException if the class of the specified element
90     * prevents it from being added to this queue
91     * @throws NullPointerException if the specified element is null
92     * @throws IllegalArgumentException if some property of the specified
93     * element prevents it from being added to this queue
94     */
95     boolean tryTransfer(E e, long timeout, TimeUnit unit)
96     throws InterruptedException;
97    
98     /**
99     * Returns {@code true} if there is at least one consumer waiting
100 jsr166 1.3 * to receive an element via {@link #take} or
101     * timed {@link #poll(long,TimeUnit) poll}.
102 jsr166 1.1 * The return value represents a momentary state of affairs.
103     *
104     * @return {@code true} if there is at least one waiting consumer
105     */
106     boolean hasWaitingConsumer();
107    
108     /**
109     * Returns an estimate of the number of consumers waiting to
110 jsr166 1.3 * receive elements via {@link #take} or timed
111     * {@link #poll(long,TimeUnit) poll}. The return value is an
112     * approximation of a momentary state of affairs, that may be
113     * inaccurate if consumers have completed or given up waiting.
114     * The value may be useful for monitoring and heuristics, but
115     * not for synchronization control. Implementations of this
116 jsr166 1.1 * method are likely to be noticeably slower than those for
117     * {@link #hasWaitingConsumer}.
118     *
119 jsr166 1.3 * @return the number of consumers waiting to receive elements
120 jsr166 1.1 */
121     int getWaitingConsumerCount();
122     }