Flow API在Java 9中对应于Reactive Streams 规范,这是一种事实上的标准。它包含了一组最小的接口,捕捉了异步发布和订阅的核心。
以下是Flow API的关键接口:
1) Flow.Publisher:它为订阅者生成要消费的项目,它只包含一个方法:subscribe(Subscriber),其目的应该是显而易见的。
void subscribe(Flow.Subscriber<? super T> subscriber)
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.
void onSubscribe(Flow.Subscription subscription) void onNext(T item) void onError(Throwable throwable) void onComplete()
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()).
void request(long n) void cancel()