首頁  >  文章  >  Java  >  Java 9中的Reactive Streams的核心介面是什麼?

Java 9中的Reactive Streams的核心介面是什麼?

WBOY
WBOY轉載
2023-08-27 20:37:021227瀏覽

Java 9中的Reactive Streams的核心接口是什么?

Java 9 在 java.util.concurrent.Flow 套件下引入了響應式串流,支援可互通的發布-訂閱 框架。它跨越非同步邊界處理非同步資料流(將元素傳遞到另一個執行緒或執行緒池),並且接收方不會被迫緩衝任意數量的數據,因此不會發生緩衝區溢位。

Flow API包含四個相互關聯的核心介面:發布者訂閱者訂閱和處理器。

語法

<strong>@FunctionalInterface
</strong>public static interface <strong>Publisher<T></strong> {
   public void <strong>subscribe</strong>(<strong>Subscriber</strong><? super T><!--? super T--> subscriber)
}
public static interface <strong>Subscriber<T></strong> {
   public void <strong>onSubscribe</strong>(Subscription subscription);
   public void <strong>onNext</strong>(T item);
   public void <strong>onError</strong>(Throwable throwable);
   public void <strong>onComplete</strong>();
}
public static interface <strong>Subscription </strong>{
   public void <strong>request</strong>(long n);
   public void <strong>cancel</strong>();
}
public static interface <strong>Processor<T, R> </strong>extends <strong>Subscriber<T></strong>, <strong>Publisher<R></strong> {
}

這四個介面:Flow.PublisherFlow.Processor、Flow.Subscriber# 和 Flow 。與反應流規範相關的訂閱。 發布者介面有subscribe()方法,訂閱cancel()request() 方法, 訂閱者onSubscribe()onNext()onError() >onComplete() 方法。 處理器介面實作Flow的所有方法。發布者Flow.Subscriber 介面。

以上是Java 9中的Reactive Streams的核心介面是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除