Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Parallel Print Hanging the console

Tags:

foreach

scala

par

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?

like image 519
Israel Zinc Avatar asked Feb 10 '17 04:02

Israel Zinc


1 Answers

It is better to post whole the program wich is hanged.

I've just tested it with scala 2.12.8 and jvm 1.8.0_161:

object MainClassss {
  def main(args: Array[String]): Unit = {
    val v = Vector.range(0,10)
    (0 to 999999).foreach(_ => v.par.foreach(println))
  }
}

the program has executed well with output and hasn't hanged.

If your program reproduced the aforementioned issue you need to take a thread dump by:

$ jstack <PID>

where PID is the process id.

Or you can take by jvisualvm jvm tool.

You can analyze why your program is hanged owing to caught thread dump if the program has some code blocking it from execution.

like image 139
Matthew I. Avatar answered Nov 05 '22 02:11

Matthew I.