Flow API在Java 9中對應到Reactive Streams 規範,這是事實上的標準。它包含了一組最小的接口,捕捉了非同步發布和訂閱的核心。
以下是Flow API的關鍵介面:
1) Flow.Publisher:它為訂閱者產生要消費的項目,它只包含一個方法:subscribe(Subscriber),其目的應該是顯而易見的。
<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>
以上是在Java 9中執行Flow API的步驟是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!