在Java 9中,我們如何實作SubmissionPublisher類別?
從Java 9 開始,我們可以透過引入四個核心介面來建立Reactive Streams:Publisher 、Subscriber、Subscription、Processor 和一個具體類別:實作Publisher 的SubmissionPublisher介面。每個介面都扮演不同的角色,對應於響應式流的原則。我們可以使用 SubmissionPublisher 類別的 submit() 方法將提供的項目發佈給每個訂閱者。
語法
<strong>public class SubmissionPublisher<T> extends Object implements Flow.Publisher<T>, AutoCloseable</strong>
在下面的範例中,我們可以實作SubmissionPublisher 類別
範例
import java.util.concurrent.Flow.Subscriber; import java.util.concurrent.Flow.Subscription; import java.util.concurrent.SubmissionPublisher; class MySubscriber<T> implements <strong>Subscriber<T></strong> { private <strong>Subscription </strong>subscription; private String name; public MySubscriber(String name) { this.name = name; } <strong>@Override</strong> public void <strong>onComplete()</strong> { System.out.println(name + ": onComplete"); } <strong>@Override</strong> public void <strong>onError</strong>(Throwable t) { System.out.println(name + ": onError"); t.printStackTrace(); } <strong> @Override</strong> public void <strong>onNext</strong>(T msg) { System.out.println(name + ": " + msg.toString() + " received in onNext"); subscription.<strong>request</strong>(1); } <strong>@Override</strong> public void <strong>onSubscribe</strong>(Subscription subscription) { System.out.println(name + ": onSubscribe"); this.subscription = subscription; subscription.<strong>request</strong>(1); } } <strong>// Main class</strong> public class FlowTest { public static void main(String args[]) { <strong>SubmissionPublisher<String></strong> publisher = new <strong>SubmissionPublisher</strong><>(); MySubscriber<String> subscriber = new MySubscriber<>("Mine"); MySubscriber<String> subscriberYours = new MySubscriber<>("Yours"); MySubscriber<String> subscriberHis = new MySubscriber<>("His"); MySubscriber<String> subscriberHers = new MySubscriber<>("Her"); publisher.<strong>subscribe</strong>(subscriber); publisher.<strong>subscribe</strong>(subscriberYours); publisher.<strong>subscribe</strong>(subscriberHis); publisher.<strong>subscribe</strong>(subscriberHers); publisher.<strong>submit</strong>("One"); publisher.<strong>submit</strong>("Two"); publisher.<strong>submit</strong>("Three"); publisher.<strong>submit</strong>("Four"); publisher.<strong>submit</strong>("Five"); try { Thread.sleep(1000); } catch(InterruptedException e) { e.printStackTrace(); } publisher.close(); } }
輸出
<strong>Yours: onSubscribe His: onSubscribe Mine: onSubscribe His: One received in onNext Yours: One received in onNext Mine: One received in onNext Yours: Two received in onNext His: Two received in onNext Yours: Three received in onNext Mine: Two received in onNext Yours: Four received in onNext His: Three received in onNext Yours: Five received in onNext Mine: Three received in onNext Her: onSubscribe His: Four received in onNext Her: One received in onNext Mine: Four received in onNext Her: Two received in onNext His: Five received in onNext Her: Three received in onNext Mine: Five received in onNext Her: Four received in onNext Her: Five received in onNext Yours: onComplete His: onComplete Mine: onComplete Her: onComplete</strong>
以上是在Java 9中,我們如何實作SubmissionPublisher類別?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。
