+str Add Source#mergePrioritizedN. (#31189)

This commit is contained in:
kerr 2022-03-14 15:05:08 +08:00 committed by GitHub
parent 9f7f7027f6
commit cbfed3b1de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 255 additions and 6 deletions

View file

@ -7,6 +7,7 @@ package jdocs.stream.operators;
import akka.Done;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.japi.Pair;
import akka.japi.pf.PFBuilder;
import akka.stream.javadsl.Flow;
@ -202,6 +203,24 @@ class SourceOrFlow {
// #mergePrioritized
}
void mergePrioritizedNExample() {
// #mergePrioritizedN
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
Source<Integer, NotUsed> sourceC = Source.from(Arrays.asList(100, 200, 300, 400));
List<Pair<Source<Integer, ?>,Integer>> sourcesAndPriorities = Arrays.asList(
new Pair<>(sourceA, 9900),
new Pair<>(sourceB, 99),
new Pair<>(sourceC, 1));
Source.mergePrioritizedN(sourcesAndPriorities, false).runForeach(System.out::println, system);
// prints e.g. 1, 100, 2, 3, 4, 10, 20, 30, 40, 200, 300, 400 since both sources have their
// first element ready and
// the left sourceA has higher priority - if both sources have elements ready, sourceA has a 99%
// chance of being picked next
// while sourceB has a 0.99% chance and sourceC has a 0.01% chance
// #mergePrioritizedN
}
void mergeSortedExample() {
// #merge-sorted
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 3, 5, 7));