
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we implement Flow API using Publisher-Subscriber in Java 9?
Flow API (java.util.concurrent.Flow) has introduced in Java 9. It helps to understand different ways in which the Publisher and Subscriber interfaces interact to perform desired operations.
Flow API consists of Publisher, Subscriber, Subscription, and Processor interfaces, which can be based on reactive stream specification.
In the below example, we can implement Flow API by using Publisher-Subscriber interfaces.
Example
import java.util.concurrent.Flow.Publisher; import java.util.concurrent.Flow.Subscriber; import java.util.concurrent.Flow.Subscription; public class FlowAPITest { public static void main(String args[]) { Publisher<Integer> publisherSync = new Publisher<Integer>() { // Create publisher @Override public void subscribe(Subscriber<? super Integer> subscriber) { for(int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + " | Publishing = " + i); subscriber.onNext(i); } subscriber.onComplete(); } }; Subscriber<Integer> subscriberSync = new Subscriber<Integer>() { // Create subscriber @Override public void onSubscribe(Subscription subscription) { } @Override public void onNext(Integer item) { System.out.println(Thread.currentThread().getName() + " | Received = " + item); try { Thread.sleep(100); } catch(InterruptedException e) { e.printStackTrace(); } } @Override public void onError(Throwable throwable) { } @Override public void onComplete() { } }; publisherSync.subscribe(subscriberSync); } }
Output
main | Publishing = 0 main | Received = 0 main | Publishing = 1 main | Received = 1 main | Publishing = 2 main | Received = 2 main | Publishing = 3 main | Received = 3 main | Publishing = 4 main | Received = 4 main | Publishing = 5 main | Received = 5 main | Publishing = 6 main | Received = 6 main | Publishing = 7 main | Received = 7 main | Publishing = 8 main | Received = 8 main | Publishing = 9 main | Received = 9
- Related Articles
- How can we implement the Subscriber interface in Java 9?
- How to implement reactive streams using Flow API in Java 9?
- How can we implement methods of Stream API in Java 9?
- How can we implement a JSON array using Streaming API in Java?
- How can we implement the SubmissionPublisher class in Java 9?
- How can we implement a map in JShell in Java 9?
- How can we Implement a Stack using Queue in Java?
- What are the steps to execute Flow API in Java 9?
- How can we Implement a Queue using Stack in Java?\n
- How can we implement right click menu using JPopupMenu in Java?
- How can we read & write a file using Gson streaming API in Java?
- How can we update an existing JSON data using javax.json API in Java?
- How can we implement a JToggleButton in Java?
- How can we implement transparent JDialog in Java?
- How can we implement editable JComboBox in Java?

Advertisements