I am new to this Scala world and I am trying some exercises from a book. So, I have an example that print a vector in sequential and parallel fashion. The former works perfectly and the later hangs the console.
Code
val v = Vector.range(0, 10)
v.foreach(println)
Code output
0123456789
But if I use the same code, but instead of using foearch, use par, it freezes the console
val v = Vector.range(0,10)
v.par.foreach(println)
The book I am using says that the output should be something like:
5678901234
But it hangs and the program never finishes.
Can someone explain me why?
{ x.par }
. – Orphanagescala -Yrepl-class-based
(per this comment), but everything else I tried (including wrapping various parts of the command in braces) did not work. – Perfumerclass X { Vector.range(0,10). par.foreach(print) } ; new X
, but this doesn'tobject X { Vector.range(0,10). par.foreach(print) } ; X
. – Perfumerimport scala.concurrent.forkjoin.ForkJoinPool; import scala.collection.parallel.ForkJoinTaskSupport; var v = Vector.range(0,10).par; v.tasksupport = new ForkJoinTaskSupport(new ForkJoinPool(4)); v.foreach(print)
. I thought maybe creating the pool explicitly would help, but it seems to still be lazily evaluated, and I still get a deadlock. – Perfumer