ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Flow.java
Revision: 1.22
Committed: Sun Jan 25 15:19:40 2015 UTC (9 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.21: +1 -1 lines
Log Message:
Minor improvements

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/publicdomain/zero/1.0/
5     */
6    
7     package java.util.concurrent;
8 jsr166 1.9
9     import java.util.ArrayList;
10 dl 1.1 import java.util.function.Consumer;
11     import java.util.function.Function;
12     import java.util.stream.Stream;
13    
14     /**
15     * Interrelated interfaces and static methods for establishing
16     * flow-controlled components in which {@link Publisher Publishers}
17     * produce items consumed by one or more {@link Subscriber
18     * Subscribers}, each managed by a {@link Subscription
19 dl 1.14 * Subscription}.
20 jsr166 1.2 *
21 dl 1.1 * <p>These interfaces correspond to the <a
22     * href="http://www.reactive-streams.org/"> reactive-streams</a>
23 dl 1.14 * specification. They apply in both concurrent and distributed
24     * asynchronous settings: All (seven) methods are defined in {@code
25 jsr166 1.20 * void} "one-way" message style. Communication relies on a simple form
26 dl 1.14 * of flow control (method {@link Subscription#request}) that can be
27     * used to avoid resource management problems that may otherwise occur
28     * in "push" based systems.
29 dl 1.1 *
30 dl 1.10 * <p><b>Examples.</b> A {@link Publisher} usually defines its own
31     * {@link Subscription} implementation; constructing one in method
32     * {@code subscribe} and issuing it to the calling {@link
33     * Subscriber}. It publishes items to the subscriber asynchronously,
34     * normally using an {@link Executor}. For example, here is a very
35     * simple publisher that only issues (when requested) a single {@code
36 dl 1.16 * TRUE} item to a single subscriber. Because the subscriber receives
37     * only a single item, this class does not use buffering and ordering
38     * control required in most implementations (for example {@link
39     * SubmissionPublisher}).
40 dl 1.10 *
41     * <pre> {@code
42     * class OneShotPublisher implements Publisher<Boolean> {
43 dl 1.17 * final ExecutorService executor = ForkJoinPool.commonPool(); // daemon-based
44 dl 1.16 * boolean subscribed = false; // true after first subscribe
45     * public synchronized void subscribe(Subscriber<? super Boolean> subscriber) {
46     * if (subscribed)
47     * subscriber.onError(new IllegalStateException()); // only one allowed
48     * else {
49     * subscribed = true;
50 dl 1.11 * subscriber.onSubscribe(new OneShotSubscription(subscriber, executor));
51 dl 1.16 * }
52 dl 1.10 * }
53     * static class OneShotSubscription implements Subscription {
54     * final Subscriber<? super Boolean> subscriber;
55 dl 1.17 * final ExecutorService executor;
56     * Future<?> future; // to allow cancellation
57 dl 1.16 * boolean completed = false;
58 dl 1.10 * OneShotSubscription(Subscriber<? super Boolean> subscriber,
59 dl 1.17 * ExecutorService executor) {
60 dl 1.10 * this.subscriber = subscriber;
61     * this.executor = executor;
62     * }
63     * public synchronized void request(long n) {
64 dl 1.16 * if (n != 0 && !completed) {
65 dl 1.10 * completed = true;
66 dl 1.21 * if (n < 0) {
67     * IllegalStateException ex = new IllegalStateException();
68 dl 1.22 * executor.execute(() -> subscriber.onError(ex));
69 dl 1.21 * }
70     * else {
71     * future = executor.submit(() -> {
72 dl 1.16 * subscriber.onNext(Boolean.TRUE);
73     * subscriber.onComplete();
74 dl 1.21 * });
75     * }
76 dl 1.10 * }
77     * }
78 dl 1.15 * public synchronized void cancel() {
79 dl 1.17 * completed = true;
80     * if (future != null) future.cancel(false);
81 dl 1.15 * }
82 dl 1.10 * }
83     * }}</pre>
84     *
85 jsr166 1.13 * <p>A {@link Subscriber} arranges that items be requested and
86 dl 1.10 * processed. Items (invocations of {@link Subscriber#onNext}) are
87     * not issued unless requested, but multiple items may be requested.
88     * Many Subscriber implementations can arrange this in the style of
89     * the following example, where a buffer size of 1 single-steps, and
90     * larger sizes usually allow for more efficient overlapped processing
91     * with less communication; for example with a value of 64, this keeps
92     * total outstanding requests between 32 and 64. (See also {@link
93     * #consume(long, Publisher, Consumer)} that automates a common case.)
94 dl 1.14 * Because Subscriber method invocations for a given {@link
95     * Subscription} are strictly ordered, there is no need for these
96     * methods to use locks or volatiles unless a Subscriber maintains
97     * multiple Subscriptions (in which case it is better to instead
98     * define multiple Subscribers, each with its own Subscription).
99 dl 1.10 *
100     * <pre> {@code
101     * class SampleSubscriber<T> implements Subscriber<T> {
102     * final Consumer<? super T> consumer;
103     * Subscription subscription;
104     * final long bufferSize;
105     * long count;
106     * SampleSubscriber(long bufferSize, Consumer<? super T> consumer) {
107     * this.bufferSize = bufferSize;
108     * this.consumer = consumer;
109     * }
110     * public void onSubscribe(Subscription subscription) {
111     * (this.subscription = subscription).request(bufferSize);
112     * count = bufferSize - bufferSize / 2; // re-request when half consumed
113     * }
114     * public void onNext(T item) {
115     * if (--count <= 0)
116     * subscription.request(count = bufferSize - bufferSize / 2);
117     * consumer.accept(item);
118     * }
119     * public void onError(Throwable ex) { ex.printStackTrace(); }
120     * public void onComplete() {}
121     * }}</pre>
122 dl 1.1 *
123 dl 1.18 * <p>The default value of {@link #defaultBufferSize} may provide a
124     * useful starting point for choosing request sizes and capacities in
125     * Flow components based on expected rates, resources, and usages.
126     * Or, when flow control is known to be always inapplicable, a
127     * subscriber may initially request an effectively unbounded number of
128     * items, as in:
129 dl 1.12 *
130     * <pre> {@code
131     * class UnboundedSubscriber<T> implements Subscriber<T> {
132     * public void onSubscribe(Subscription subscription) {
133     * subscription.request(Long.MAX_VALUE); // effectively unbounded
134     * }
135     * public void onNext(T item) { use(item); }
136     * public void onError(Throwable ex) { ex.printStackTrace(); }
137     * public void onComplete() {}
138     * void use(T item) { ... }
139     * }}</pre>
140 jsr166 1.13 *
141 dl 1.1 * @author Doug Lea
142     * @since 1.9
143     */
144     public final class Flow {
145    
146     private Flow() {} // uninstantiable
147    
148     /**
149     * A producer of items (and related control messages) received by
150     * Subscribers. Each current {@link Subscriber} receives the same
151 dl 1.14 * items (via method {@code onNext}) in the same order, unless
152     * drops or errors are encountered. If a Publisher encounters an
153 dl 1.15 * error that does not allow items to be issued to a Subscriber,
154     * that Subscriber receives {@code onError}, and then receives no
155     * further messages. Otherwise, when it is known that no further
156     * messages will be issued to it, a subscriber receives {@code
157     * onComplete}. Publishers ensure that Subscriber method
158 dl 1.14 * invocations for each subscription are strictly ordered in <a
159     * href="package-summary.html#MemoryVisibility"><i>happens-before</i></a>
160     * order.
161     *
162 dl 1.15 * <p>Publishers may vary in policy about whether drops (failures
163     * to issue an item because of resource limitations) are treated
164     * as unrecoverable errors. Publishers may also vary about
165     * whether Subscribers receive items that were produced or
166     * available before they subscribed.
167 dl 1.1 *
168     * @param <T> the published item type
169     */
170     @FunctionalInterface
171     public static interface Publisher<T> {
172     /**
173     * Adds the given Subscriber if possible. If already
174     * subscribed, or the attempt to subscribe fails due to policy
175 dl 1.14 * violations or errors, the Subscriber's {@code onError}
176     * method is invoked with an {@link IllegalStateException}.
177     * Otherwise, the Subscriber's {@code onSubscribe} method is
178     * invoked with a new {@link Subscription}. Subscribers may
179     * enable receiving items by invoking the {@code request}
180     * method of this Subscription, and may unsubscribe by
181     * invoking its {@code cancel} method.
182 dl 1.1 *
183     * @param subscriber the subscriber
184     * @throws NullPointerException if subscriber is null
185     */
186     public void subscribe(Subscriber<? super T> subscriber);
187     }
188    
189     /**
190 dl 1.14 * A receiver of messages. The methods in this interface are
191     * invoked in strict sequential order for each {@link
192     * Subscription}.
193 dl 1.1 *
194     * @param <T> the subscribed item type
195     */
196     public static interface Subscriber<T> {
197     /**
198     * Method invoked prior to invoking any other Subscriber
199     * methods for the given Subscription. If this method throws
200     * an exception, resulting behavior is not guaranteed, but may
201     * cause the Subscription to be cancelled.
202     *
203 dl 1.15 * <p>Typically, implementations of this method invoke {@code
204     * subscription.request} to enable receiving items.
205 dl 1.1 *
206     * @param subscription a new subscription
207     */
208     public void onSubscribe(Subscription subscription);
209    
210     /**
211     * Method invoked with a Subscription's next item. If this
212     * method throws an exception, resulting behavior is not
213     * guaranteed, but may cause the Subscription to be cancelled.
214     *
215     * @param item the item
216     */
217     public void onNext(T item);
218    
219     /**
220     * Method invoked upon an unrecoverable error encountered by a
221     * Publisher or Subscription, after which no other Subscriber
222     * methods are invoked by the Subscription. If this method
223     * itself throws an exception, resulting behavior is
224     * undefined.
225     *
226     * @param throwable the exception
227     */
228     public void onError(Throwable throwable);
229    
230     /**
231 dl 1.15 * Method invoked when it is known that no additional
232     * Subscriber method invocations will occur for a Subscription
233     * that is not already terminated by error, after which no
234     * other Subscriber methods are invoked by the Subscription.
235     * If this method throws an exception, resulting behavior is
236 dl 1.14 * undefined.
237 dl 1.1 */
238     public void onComplete();
239     }
240    
241     /**
242 dl 1.14 * Message control linking a {@link Publisher} and {@link
243     * Subscriber}. Subscribers receive items only when requested,
244     * and may cancel at any time. The methods in this interface are
245     * intended to be invoked only by their Subscribers; usages in
246     * other contexts have undefined effects.
247 dl 1.1 */
248     public static interface Subscription {
249     /**
250     * Adds the given number {@code n} of items to the current
251     * unfulfilled demand for this subscription. If {@code n} is
252 dl 1.14 * negative, the Subscriber will receive an {@code onError}
253 jsr166 1.19 * signal with an {@link IllegalArgumentException} argument.
254     * Otherwise, the Subscriber will receive up to {@code n}
255     * additional {@code onNext} invocations (or fewer if
256     * terminated).
257 dl 1.1 *
258     * @param n the increment of demand; a value of {@code
259     * Long.MAX_VALUE} may be considered as effectively unbounded
260     */
261     public void request(long n);
262    
263     /**
264 dl 1.14 * Causes the Subscriber to (eventually) stop receiving
265 dl 1.15 * messages. Implementation is best-effort -- additional
266     * messages may be received after invoking this method. A
267     * cancelled subscription need not ever receive an {@code
268     * onComplete} signal.
269 dl 1.1 */
270     public void cancel();
271     }
272    
273     /**
274 jsr166 1.2 * A component that acts as both a Subscriber and Publisher.
275 dl 1.1 *
276 jsr166 1.2 * @param <T> the subscribed item type
277     * @param <R> the published item type
278     */
279 jsr166 1.5 public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> {
280 dl 1.1 }
281    
282     // Support for static methods
283    
284 dl 1.18 static final int DEFAULT_BUFFER_SIZE = 256;
285    
286     /**
287     * Returns a default value for Publisher or Subscriber buffering,
288     * that may be used in the absence of other constraints.
289     *
290     * @implNote
291     * The current value returned is 256.
292     *
293     * @return the buffer size value
294     */
295     public static int defaultBufferSize() {
296     return DEFAULT_BUFFER_SIZE;
297     }
298 dl 1.1
299 jsr166 1.19 abstract static class CompletableSubscriber<T,U>
300     implements Subscriber<T>, Consumer<T>
301     {
302 dl 1.1 final CompletableFuture<U> status;
303     Subscription subscription;
304 dl 1.10 long requestSize;
305 jsr166 1.2 long count;
306 dl 1.10 CompletableSubscriber(long bufferSize, CompletableFuture<U> status) {
307 dl 1.1 this.status = status;
308 dl 1.10 this.requestSize = bufferSize;
309 dl 1.1 }
310     public final void onSubscribe(Subscription subscription) {
311     (this.subscription = subscription).request(requestSize);
312 dl 1.10 count = requestSize -= (requestSize >>> 1);
313 dl 1.1 }
314 jsr166 1.2 public final void onError(Throwable ex) {
315 dl 1.1 status.completeExceptionally(ex);
316     }
317     public void onNext(T item) {
318 dl 1.10 try {
319     if (--count <= 0)
320     subscription.request(count = requestSize);
321     accept(item);
322     } catch (Throwable ex) {
323     status.completeExceptionally(ex);
324 dl 1.1 }
325     }
326     }
327 jsr166 1.2
328 dl 1.1 static final class ConsumeSubscriber<T> extends CompletableSubscriber<T,Void> {
329     final Consumer<? super T> consumer;
330 dl 1.10 ConsumeSubscriber(long bufferSize,
331 dl 1.1 CompletableFuture<Void> status,
332     Consumer<? super T> consumer) {
333 dl 1.10 super(bufferSize, status);
334 dl 1.1 this.consumer = consumer;
335     }
336     public void accept(T item) { consumer.accept(item); }
337     public void onComplete() { status.complete(null); }
338     }
339    
340     /**
341     * Creates and subscribes a Subscriber that consumes all items
342     * from the given publisher using the given Consumer function, and
343 dl 1.10 * using the given bufferSize for buffering. Returns a
344 dl 1.1 * CompletableFuture that is completed normally when the publisher
345 dl 1.14 * signals {@code onComplete}, or completed exceptionally upon any
346     * error, including an exception thrown by the Consumer (in which
347     * case the subscription is cancelled if not already terminated).
348 dl 1.1 *
349 jsr166 1.4 * @param <T> the published item type
350 dl 1.10 * @param bufferSize the request size for subscriptions
351 dl 1.1 * @param publisher the publisher
352     * @param consumer the function applied to each onNext item
353     * @return a CompletableFuture that is completed normally
354     * when the publisher signals onComplete, and exceptionally
355 jsr166 1.7 * upon any error
356 dl 1.1 * @throws NullPointerException if publisher or consumer are null
357 dl 1.10 * @throws IllegalArgumentException if bufferSize not positive
358 dl 1.1 */
359     public static <T> CompletableFuture<Void> consume(
360 dl 1.10 long bufferSize, Publisher<T> publisher, Consumer<? super T> consumer) {
361     if (bufferSize <= 0L)
362     throw new IllegalArgumentException("bufferSize must be positive");
363 dl 1.1 if (publisher == null || consumer == null)
364     throw new NullPointerException();
365     CompletableFuture<Void> status = new CompletableFuture<>();
366     publisher.subscribe(new ConsumeSubscriber<T>(
367 dl 1.10 bufferSize, status, consumer));
368 dl 1.1 return status;
369     }
370    
371     /**
372     * Equivalent to {@link #consume(long, Publisher, Consumer)}
373 dl 1.18 * with {@link #defaultBufferSize}.
374 dl 1.1 *
375 jsr166 1.4 * @param <T> the published item type
376 dl 1.1 * @param publisher the publisher
377     * @param consumer the function applied to each onNext item
378     * @return a CompletableFuture that is completed normally
379     * when the publisher signals onComplete, and exceptionally
380 jsr166 1.7 * upon any error
381 dl 1.1 * @throws NullPointerException if publisher or consumer are null
382     */
383     public static <T> CompletableFuture<Void> consume(
384     Publisher<T> publisher, Consumer<? super T> consumer) {
385 dl 1.18 return consume(defaultBufferSize(), publisher, consumer);
386 dl 1.1 }
387    
388     /**
389     * Temporary implementation for Stream, collecting all items
390     * and then applying stream operation.
391     */
392     static final class StreamSubscriber<T,R> extends CompletableSubscriber<T,R> {
393     final Function<? super Stream<T>, ? extends R> fn;
394     final ArrayList<T> items;
395 dl 1.10 StreamSubscriber(long bufferSize,
396 dl 1.1 CompletableFuture<R> status,
397     Function<? super Stream<T>, ? extends R> fn) {
398 dl 1.10 super(bufferSize, status);
399 dl 1.1 this.fn = fn;
400     this.items = new ArrayList<T>();
401     }
402     public void accept(T item) { items.add(item); }
403     public void onComplete() { status.complete(fn.apply(items.stream())); }
404     }
405    
406     /**
407     * Creates and subscribes a Subscriber that applies the given
408 dl 1.10 * stream operation to items, and uses the given bufferSize for
409 dl 1.1 * buffering. Returns a CompletableFuture that is completed
410     * normally with the result of this function when the publisher
411 dl 1.14 * signals {@code onComplete}, or is completed exceptionally upon
412     * any error.
413 dl 1.1 *
414     * <p><b>Preliminary release note:</b> Currently, this method
415     * collects all items before executing the stream
416     * computation. Improvements are pending Stream integration.
417     *
418 jsr166 1.4 * @param <T> the published item type
419     * @param <R> the result type of the stream function
420 dl 1.10 * @param bufferSize the request size for subscriptions
421 dl 1.1 * @param publisher the publisher
422     * @param streamFunction the operation on elements
423     * @return a CompletableFuture that is completed normally with the
424     * result of the given function as result when the publisher signals
425 jsr166 1.7 * onComplete, and exceptionally upon any error
426 dl 1.1 * @throws NullPointerException if publisher or function are null
427 dl 1.10 * @throws IllegalArgumentException if bufferSize not positive
428 dl 1.1 */
429     public static <T,R> CompletableFuture<R> stream(
430 dl 1.10 long bufferSize, Publisher<T> publisher,
431 dl 1.1 Function<? super Stream<T>, ? extends R> streamFunction) {
432 dl 1.10 if (bufferSize <= 0L)
433     throw new IllegalArgumentException("bufferSize must be positive");
434 dl 1.1 if (publisher == null || streamFunction == null)
435     throw new NullPointerException();
436     CompletableFuture<R> status = new CompletableFuture<>();
437     publisher.subscribe(new StreamSubscriber<T,R>(
438 dl 1.10 bufferSize, status, streamFunction));
439 dl 1.1 return status;
440     }
441 jsr166 1.2
442 dl 1.1 /**
443     * Equivalent to {@link #stream(long, Publisher, Function)}
444 dl 1.18 * with {@link #defaultBufferSize}.
445 dl 1.1 *
446 jsr166 1.4 * @param <T> the published item type
447     * @param <R> the result type of the stream function
448 dl 1.1 * @param publisher the publisher
449     * @param streamFunction the operation on elements
450     * @return a CompletableFuture that is completed normally with the
451     * result of the given function as result when the publisher signals
452 jsr166 1.7 * onComplete, and exceptionally upon any error
453 dl 1.1 * @throws NullPointerException if publisher or function are null
454     */
455     public static <T,R> CompletableFuture<R> stream(
456 jsr166 1.2 Publisher<T> publisher,
457 dl 1.1 Function<? super Stream<T>,? extends R> streamFunction) {
458 dl 1.18 return stream(defaultBufferSize(), publisher, streamFunction);
459 dl 1.1 }
460     }