ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/TransferQueue.java
Revision: 1.5
Committed: Tue Jul 21 00:15:14 2009 UTC (14 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +3 -3 lines
Log Message:
j.u.c. coding standards

File Contents

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