The Scala standard library provides such a synchronization mechanism as the scala.concurrent.SyncVar[T] class.
This Scala REPL session demonstrates how it works:
scala> import concurrent._ import concurrent._ scala> import ops._ import ops._
I import ops._ to create another Thread .
scala> val syncInt = new SyncVar[Int] syncInt: scala.concurrent.SyncVar[Int] = scala.concurrent.SyncVar@17823918 scala> spawn { println("got %d" format syncInt.get) }
I am creating another thread. get blocked until there is a value in syncInt .
scala> syncInt.isSet res1: Boolean = false scala> syncInt.set(103) scala> got 103
The above was printed by the stream we created earlier.
scala> syncInt.isSet res3: Boolean = true scala> syncInt.get res4: Int = 103
source share