The year 2020 was hard and exhausting for everyone around the globe. A lot of people may have experienced feelings of anxiety, depression, or frustration.
The first example is a baseline—you want to run your code on the currently executing thread. It’s a pretty simple scenario, shown first in JDK8 and then in Reactor.
Here’s the reactor example:
Nothing special to see here—just iterate from 0 to 9 and print a random number. The next example is more complicated. What if you want to print the numbers on a different thread? Here’s an example using JDK8 and ForkJoinPool:
Now here’s the example using reactor:
The import thing to note here is the subscribeOn operating. In this example, it’s telling reactor to subscribe on a worker provided by the parallel scheduler. The blockLast operator is called instead of subscribe because the code is running in a different thread we must wait until it’s completed or the JVM would exit.
Another thing to note is that the subscriber of the publisher can choose to run it on different threads independent of the publisher’s creator.
In this example the ‘observable’ object was returned by a method. The subscriber could either run on the current thread or do what the example did and run it using a scheduler on potentially another thread.
This next example shows how to run each random number generation in a different thread. Here’s what it looks like using JDK8’s ForkJoinPool:
This time we had to add the ‘ForkJoinTask’ objects a list and then block on them to make sure each task was executed.
Here is the equivalent example using reactor-core:
This example uses a flatMap to map and merge the results of another Publisher together. The publishers created inside the flatMap—in this case a Mono that prints a message—is told to subscribe using a scheduler. This causes each of these different Monos to run on a different thread. The main publisher is then blocked until everything is finished. What’s interesting about this is that the Monos could be anything—a synchronous Mono that prints a message, a runnable that prints a message on a different thread, or an off-box network call. They are all treated the same.
The final example does work on two different threads. The first thread generates a random number, prints it and then makes it available to another thread. The second thread then adds up the random incoming numbers printing an intermediate total, and then a final total after it gets 10 random numbers. Here’s what the example looks like in JDK8 using the ForkJoinPool:
Things are starting to get trickier here. We must use a latch and count how many receive to make sure that we have ten items. Also, we needed us some kind of thread-safe queue to send data between the threads.
Here’s what the example looks like with reactor-core:
Before we get to the publishOn operators let’s look at the map, and reduce operators. The map operator takes an integer generated by range, and then returns the random number that is generated. This is then passed to the reduce operator. In this case we are just adding two numbers, and it will add all the different random number as they are generated. At the end we call block to wait until everything is done.
This example uses the publishOn method. The first publishOn method is before the map operator. This makes the map operators happen on another thread. There is another publishOn right after the map operator and right before a reduce operator. This will cause all the reduce operations to also happen on a different thread. There isn’t another publishOn before the doOnNext, so it happens on the same thread of the reduce operator.
It is in more complex examples like this that Reactive Streams schedulers really shine. Rather than having to spend time reasoning about complex and potentially buggy multithreaded code using Java's concurrency utilities, having understood the scheduleOn and publishOn operators it is a simple matter to introduce parallelism where is it needed in a way that it both concise and easy to reason about.
John Deere uses Intel’s artificial intelligence technology to solve age-old costly welding product manufacturing challenges. A solution is currently being tested that uses computer vision to…
Since becoming widows, the Lady Time ladies have tried drinking their sorrows away new hobbies, gone on dates with soon-to-be charged felons, and (accidentally) droned each other’s ears off, but they…
If a credit card has fraudulent charges to a online company that sells gift cards.Then the charges get reported to bank by the card owner.Will the bank contact that company even if that online…