Last active
February 27, 2024 12:28
-
-
Save PrabhatKJena/1f565460d21ad48a5ff0cab82a44b6a1 to your computer and use it in GitHub Desktop.
Reactive Implementation demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.Random; | |
import java.util.function.Consumer; | |
class ReactiveImplDemo { | |
public static void main(String[] args) throws IOException { | |
System.out.println("Start"); | |
ReactiveSource.getIntFlux(20).subscribe(x -> System.out.println("Subscriber 1 received int: " + x)); | |
ReactiveSource.getIntFlux(12).subscribe(x -> System.out.println(" Subscriber 2 received int: " + x)); | |
System.out.println("End. Press any key to continue"); | |
System.in.read(); | |
} | |
} | |
class ReactiveSource { | |
public static Flux<Integer> getIntFlux(int limit) { | |
final Flux<Integer> integerFlux = new Flux<>(); | |
new Thread(() -> { | |
final DownStream downStream = new DownStream(); | |
int i = 1; | |
while (i <= limit) { | |
int anInt = downStream.generateNextInt(); | |
integerFlux.consumer.accept(anInt); | |
i++; | |
} | |
}).start(); | |
return integerFlux; | |
} | |
} | |
class Flux<T> { | |
Consumer<T> consumer; | |
public void subscribe(Consumer<T> consumer) { | |
this.consumer = consumer; | |
} | |
} | |
class DownStream { | |
final Random random = new Random(); | |
public int generateNextInt() { | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
return random.nextInt(1000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output