Reputation: 139
I want to merge three Maybes together, as follows:
return Maybe.merge(
aOneSecondTask(),
aFiveSecondTask(),
aTenSecondTask()
).firstElement();
Each of the tasks does a Thread.sleep()
for the appropriate amount of time, then logs the current time just before returning.
When I run this code, the logs look like this:
Starting at 13:26:38
Finishing one-second task at 13:26:39
Finishing five-second task at 13:26:44
Finishing ten-second task at 13:26:54
The docs for Maybe suggest that this method should run all the Maybes at once. I'd therefore expect the entire thing to take no more than 10 seconds. Instead, it takes 16 seconds - each task doesn't start until the previous one is finished.
How can I fire off all three tasks simultaneously?
Upvotes: 1
Views: 192
Reputation: 1187
You probably have only a single-threaded scheduler, so while your Maybe
s are scheduled to run concurrently, they actually run in series. Don't use blocking calls like Thread.sleep()
in a reactive environment. In this case, as you are just simulating a slow process, use something like delay()
to add the required delay to each Maybe
.
Upvotes: 1