Home > front end >  foreach in Scala parallel collections
foreach in Scala parallel collections

Time:03-03

I have this code evaluated with Ammonite:

$ amm
Welcome to the Ammonite Repl 2.5.1 (Scala 2.13.8 Java 17.0.1)
@ import $ivy.`org.scala-lang.modules::scala-parallel-collections:1.0.4`
@ import scala.collection.parallel.CollectionConverters._
@ Seq(1,2).foreach { x => Thread sleep x*1000; println(s"Fin $x") };println("Fin") 
Fin 1
Fin 2
Fin

It completes ok. If I add the par to parallelize, then it never finishes:

@ Seq(1,2).par.foreach { x => Thread sleep x*1000; println(s"Fin $x") };println("Fin")

Is this a bug? With Scala 2.12 I get the same behaviour.

CodePudding user response:

You're having this problem due this bug with Scala's lambda encoding that will also happen in the Scala REPL.

The bug on the Scala side: https://github.com/scala/scala-parallel-collections/issues/34

The corresponding ammonite bug report is here: https://github.com/com-lihaoyi/Ammonite/issues/556

You can work around this in two ways that I'm aware of. The first is to put your parallel work inside of an object e.g.

  object work {
    def execute() = Seq(1, 2).foreach { x =>
      Thread.sleep(x * 1000); println(s"Fin $x")
    }; println("Fin")
  }
  work.execute

I believe running Ammonite with amm --class-based should also do the trick, but I'm not able to test this right now.

  • Related