ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/TransferQueue.java
Revision: 1.13
Committed: Sun Jan 18 20:17:33 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +1 -0 lines
Log Message:
exactly one blank line before and after package statements

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