ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TransferQueue.java
Revision: 1.1
Committed: Sat Jul 25 01:06:20 2009 UTC (14 years, 10 months ago) by jsr166
Branch: MAIN
Log Message:
branch jsr166y into java.util.concurrent

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