Rumah > Java > javaTutorial > Bagaimanakah kita melaksanakan kelas SubmissionPublisher dalam Java 9?

Bagaimanakah kita melaksanakan kelas SubmissionPublisher dalam Java 9?

WBOY
Lepaskan: 2023-09-02 19:05:02
ke hadapan
1013 orang telah melayarinya

在Java 9中,我们如何实现SubmissionPublisher类?

Starting dari Java 9, kita boleh membuat aliran reactive dengan memperkenalkan empat antara muka teras: publisher, subscriber, subscription, processor dan kelas konkrit: pelaksanaan Antara muka SubmissionPublisher Penerbit. Setiap antara muka memainkan peranan yang berbeza, sepadan dengan prinsip tindak balasaliran. Kami boleh menerbitkan item yang disediakan kepada setiap pelanggan menggunakan kaedah submit() kelas SubmissionPublisher .

Syntax

<strong>public class SubmissionPublisher<T> extends Object implements Flow.Publisher<T>, AutoCloseable</strong>
Salin selepas log masuk
Dalam contoh berikut kita boleh melaksanakan

SubmissionPublisher

ClassContoh

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();
   }
}
Salin selepas log masuk

Output

<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>
Salin selepas log masuk

Atas ialah kandungan terperinci Bagaimanakah kita melaksanakan kelas SubmissionPublisher dalam Java 9?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:tutorialspoint.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan