1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain. Use, modify, and |
4 |
* redistribute this code in any way without acknowledgement. |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
import java.util.Queue; |
9 |
|
10 |
/** |
11 |
* A {@link java.util.Queue} that additionally supports operations |
12 |
* that wait for elements to exist when retrieving them, and wait for |
13 |
* space to exist when putting them. |
14 |
* |
15 |
* <p> <tt>BlockingQueues</tt> do not accept <tt>null</tt> elements. |
16 |
* Implementations throw <tt>IllegalArgumentException</tt> on attempts |
17 |
* to <tt>add</tt>, <tt>put</tt> or <tt>offer</tt> a <tt>null</tt>. A |
18 |
* <tt>null</tt> is used as a sentinel value to indicate failure of |
19 |
* <tt>poll</tt> operations. |
20 |
* |
21 |
* <p><tt>BlockingQueues</tt> may be capacity bounded. At any given |
22 |
* time they may have a <tt>remainingCapacity</tt> beyond which no |
23 |
* additional elements can be <tt>put</tt> without blocking. |
24 |
* BlockingQueues without any intrinsic capacity constraints always |
25 |
* report a remaining capacity of <tt>Integer.MAX_VALUE</tt>. |
26 |
* |
27 |
* <p> While <tt>BlockingQueues</tt> are designed to be used primarily |
28 |
* as producer-consumer queues, they additionally support the |
29 |
* <tt>Collection</tt> interface. So, for example, it is possible to |
30 |
* remove an arbitrary element from within a queue using |
31 |
* <tt>remove(x)</tt>. However, such operations are in general |
32 |
* <em>NOT</em> performed very efficiently, and are intended for only |
33 |
* occasional use, such as when a queued message is cancelled. Also, |
34 |
* the bulk operations, most notably <tt>addAll</tt> are <em>NOT</em> |
35 |
* performed atomically, so it is possible for <tt>addAll(c)</tt> to |
36 |
* fail (throwing an exception) after adding only some of the elements |
37 |
* in <tt>c</tt>. |
38 |
* |
39 |
* <p><tt>BlockingQueue</tt>s do <em>not</em> intrinsically support |
40 |
* any kind of "close" or "shutdown" operation to |
41 |
* indicate that no more items will be added. The needs and usage of |
42 |
* such features tend to be implementation-dependent. For example, a |
43 |
* common tactic is for producers to insert special |
44 |
* <em>end-of-stream</em> or <em>poison</em> objects, that are |
45 |
* interpreted accordingly when taken by consumers. |
46 |
* |
47 |
* <p> |
48 |
* Usage example, based on a typical producer-consumer scenario. |
49 |
* Note that Blocking queues can safely be used with multiple producers |
50 |
* and multiple consumers. |
51 |
* <pre> |
52 |
* class Producer implements Runnable { |
53 |
* private final BlockingQueue queue; |
54 |
* Producer(BlockingQueue q) { queue = q; } |
55 |
* public void run() { |
56 |
* try { |
57 |
* while(true) { queue.put(produce()); } |
58 |
* } |
59 |
* catch (InterruptedException ex) { ... handle ...} |
60 |
* } |
61 |
* Object produce() { ... } |
62 |
* } |
63 |
* |
64 |
* class Consumer implements Runnable { |
65 |
* private final BlockingQueue queue; |
66 |
* Concumer(BlockingQueue q) { queue = q; } |
67 |
* public void run() { |
68 |
* try { |
69 |
* while(true) { consume(queue.take()); } |
70 |
* } |
71 |
* catch (InterruptedException ex) { ... handle ...} |
72 |
* } |
73 |
* void consume(Object x) { ... } |
74 |
* } |
75 |
* |
76 |
* class Setup { |
77 |
* void main() { |
78 |
* BlockingQueue q = new SomeQueueImplementation(); |
79 |
* Producer p = new Producer(q); |
80 |
* Consumer c1 = new Consumer(q); |
81 |
* Consumer c2 = new Consumer(q); |
82 |
* new Thread(p).start(); |
83 |
* new Thread(c1).start(); |
84 |
* new Thread(c2).start(); |
85 |
* } |
86 |
* } |
87 |
* </pre> |
88 |
* |
89 |
* |
90 |
* @since 1.5 |
91 |
* @spec JSR-166 |
92 |
* @revised $Date: 2003/06/23 02:26:16 $ |
93 |
* @editor $Author: brian $ |
94 |
* @author Doug Lea |
95 |
*/ |
96 |
public interface BlockingQueue<E> extends Queue<E> { |
97 |
/** |
98 |
* Retrieve and remove the first element from the queue, waiting |
99 |
* if no objects are present on the queue. |
100 |
* @return the object |
101 |
* @throws InterruptedException if interrupted while waiting. |
102 |
*/ |
103 |
E take() throws InterruptedException; |
104 |
|
105 |
/** |
106 |
* Retrieve and remove the first element from the queue, waiting |
107 |
* if necessary up to a specified wait time if no objects are |
108 |
* present on the queue. |
109 |
* @param timeout how long to wait before giving up, in units of |
110 |
* <tt>unit</tt> |
111 |
* @param unit a TimeUnit determining how to interpret the timeout |
112 |
* parameter |
113 |
* @return the object, or <tt>null</tt> if the specified waiting |
114 |
* time elapses before an object is present. |
115 |
* @throws InterruptedException if interrupted while waiting. |
116 |
*/ |
117 |
E poll(long timeout, TimeUnit unit) |
118 |
throws InterruptedException; |
119 |
|
120 |
/** |
121 |
* Add the given object to the queue, waiting if necessary for |
122 |
* space to become available. |
123 |
* @param x the object to add |
124 |
* @throws InterruptedException if interrupted while waiting. |
125 |
*/ |
126 |
void put(E x) throws InterruptedException; |
127 |
|
128 |
/** |
129 |
* Add the given object to the queue, waiting if necessary up to a |
130 |
* specified wait time for space to become available. |
131 |
* @param x the object to add |
132 |
* @param timeout how long to wait before giving up, in units of |
133 |
* <tt>unit</tt> |
134 |
* @param unit a TimeUnit determining how to interpret the timeout |
135 |
* parameter |
136 |
* @return <tt>true</tt> if successful, or <tt>false</tt> if |
137 |
* the specified waiting time elapses before space is available. |
138 |
* @throws InterruptedException if interrupted while waiting. |
139 |
*/ |
140 |
boolean offer(E x, long timeout, TimeUnit unit) |
141 |
throws InterruptedException; |
142 |
|
143 |
/** |
144 |
* Return the number of elements that this queue can ideally (in |
145 |
* the absence of memory or resource constraints) accept without |
146 |
* blocking, or <tt>Integer.MAX_VALUE</tt> if there is no |
147 |
* intrinsic limit. Note that you <em>cannot</em> always tell if |
148 |
* an attempt to <tt>add</tt> an element will succeed by |
149 |
* inspecting <tt>remainingCapacity</tt> because it may be the |
150 |
* case that a waiting consumer is ready to <tt>take</tt> an |
151 |
* element out of an otherwise full queue. |
152 |
* @return the remaining capacity |
153 |
*/ |
154 |
int remainingCapacity(); |
155 |
|
156 |
} |