Flow API corresponds to the Reactive Streams specification in Java 9, which is a de facto standard. It contains a minimal set of interfaces that capture the core of asynchronous publishing and subscription.
The following are the key interfaces of Flow API:
1) Flow.Publisher: It generates items for subscribers to consume, and it contains only one method: subscribe(Subscriber), whose purpose should be obvious.
<strong>void subscribe(Flow.Subscriber<? super T><!--? super T--> subscriber)</strong>
2) Flow.Subscriber: It subscribes to publishers (usually only one) to receive items (via method onNext(T)), error messages (onError(Throwable)), or a signal that no more items are to be expected (onComplete()). Before any of those things happen, the publisher calls onSubscription(Subscription) method.
<strong>void onSubscribe(Flow.Subscription subscription) void onNext(T item) void onError(Throwable throwable) void onComplete()</strong>
3) Flow.Subscription: The connection between a single publisher and a single subscriber. The subscriber can use it to request more items (request (long)) or break the connection (cancel()).
<strong>void request(long n) void cancel()</strong>
The above is the detailed content of What are the steps to implement Flow API in Java 9?. For more information, please follow other related articles on the PHP Chinese website!