As described in the akka thread documentation, I tried to create a pool of workers (threads):
def balancer[In, Out](worker: Flow[In, Out, NotUsed], workerCount: Int): Flow[In, Out, NotUsed] = { import GraphDSL.Implicits._ Flow.fromGraph(GraphDSL.create() { implicit b => val balancer = b.add(Balance[In](workerCount)) val merge = b.add(Merge[Out](workerCount)) for (_ <- 1 to workerCount) { balancer ~> worker ~> merge } FlowShape(balancer.in, merge.out) }) }
then I used this function to start the thread in parallel:
def main(args: Array[String]) { val system = ActorSystem() implicit val mat = ActorMaterializer.create(system) val flow = Flow[Int].map(e => { println(e) Thread.sleep(1000)
I get the expected output 1, 2, 3, 4, 5, 6, 7, 8, 9
, but the numbers are displayed at a speed of 1 per second (without parallelism). What am I doing wrong?
source share