1

I have code like below

    val g = new Graph(vertices)

        //Firts part

        (1 to vertices).par.foreach( i => g + new Vertex(i))

       //Second part

          for (i <- 1 to edges) {
            val data = scala.io.StdIn.readLine()
            val d = data.split(" ")
            val v1 = d(0).toInt
            val v2 = d(1).toInt
            val length = d(2).toInt
            g+(v1, v2, length)
        }

I want to execute first and second part of code sequentially. At present for loop run before the all Vertex have added to g. In code + (plus) define add new instance of Vertex to MutableList.

I'am new in scala, please help

matiii
  • 316
  • 4
  • 17

3 Answers3

0

Parallel collection only parallelises the computation for the collection. The second part of the computation where you add edges should happen after the adding of the vertices.

What I would assume is that may be from parsing there are some vertices which does not exists in the vertices at all, may be some empty space or something like that.

I am not sure about what goes on while adding the vertices to the graph but the foreach on parallel should be careful if the operation should be free of side-effect. Please see this for more information. May be this is not even relevant.

Biswanath
  • 9,075
  • 12
  • 44
  • 58
0

You could wrap each call in the following

new Thread(new Runnable {
  override def run(): Unit = {
    //Code part here:
  }
}).start()

You also need to ensure that the your Graph implementation is thread safe as you will have two threads modifying it concurrently.

It doesn't look like you're returning anything from either part but if you were you could use a Future instead. See here for details.

nattyddubbs
  • 2,085
  • 15
  • 24
0

I find out the solution. I read more about add new elements to collection parallel and it isn't thread save. I replaced MutableList to fixedsize array, and I add new element by index.

Some code below:

class Graph(val end: Int) {

  private val vertices : Array[Vertex] = new Array[Vertex](end)

  def +(index: Int, v: Vertex): Unit = {
    vertices(index) = v
  }  

  (...)
}

     //Firts part
      (1 to vertices).par.foreach( i => g + (i-1,new Vertex(i))) //add new vertex to array by index
matiii
  • 316
  • 4
  • 17