Computer >> Máy Tính >  >> Lập trình >> Java

Làm cách nào chúng ta có thể triển khai lớp SubmissionPublisher trong Java 9?


Kể từ Java 9, chúng tôi có thể tạo R Luồng hoạt động bằng cách giới thiệu bốn giao diện cốt lõi: Publisher , Người đăng ký , Đăng ký , Bộ xử lý và một lớp cụ thể: SubmissionPublisher triển khai Nhà xuất bản giao diện. Mỗi giao diện đóng một vai trò khác nhau, tương ứng với các nguyên tắc của Phản ứng Luồng . Chúng tôi có thể sử dụng submit () phương thức của SubmissionPublisher lớp để xuất bản mục đã cung cấp cho từng người đăng ký.

Cú pháp

public class SubmissionPublisher<T> extends Object implements Flow.Publisher<T>, AutoCloseable

Trong ví dụ dưới đây, chúng tôi có thể triển khai SubmissionPublisher lớp học

Ví dụ

import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;
import java.util.concurrent.SubmissionPublisher;

class MySubscriber<T> implements Subscriber<T> {
   private Subscription subscription;
   private String name;
   public MySubscriber(String name) {
      this.name = name;
   }
   @Override
   public void onComplete() {
      System.out.println(name + ": onComplete");
   }
   @Override
   public void onError(Throwable t) {
      System.out.println(name + ": onError");
      t.printStackTrace();
   }
   @Override
   public void onNext(T msg) {
      System.out.println(name + ": " + msg.toString() + " received in onNext");
      subscription.request(1);
   }
   @Override
   public void onSubscribe(Subscription subscription) {
      System.out.println(name + ": onSubscribe");
      this.subscription = subscription;
      subscription.request(1);
   }
}

// Main class
public class FlowTest {
   public static void main(String args[]) {
      SubmissionPublisher<String> publisher = new SubmissionPublisher<>();
      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.subscribe(subscriber);
      publisher.subscribe(subscriberYours);
      publisher.subscribe(subscriberHis);
      publisher.subscribe(subscriberHers);

      publisher.submit("One");
      publisher.submit("Two");
      publisher.submit("Three");
      publisher.submit("Four");
      publisher.submit("Five");

      try {
         Thread.sleep(1000);
      } catch(InterruptedException e) {
         e.printStackTrace();
      }
      publisher.close();
   }
}

Đầu ra

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