Skip to content

Instantly share code, notes, and snippets.

@vasily-kirichenko
Created August 13, 2014 18:17
import scala.concurrent.stm._
object Test {
def run(n: Int) = {
val x = Ref(0)
val y = Ref(0)
(1 to n) foreach { i =>
atomic { implicit tx =>
val x1 = x()
val y1 = y()
if (x() != y()) retry
x() = x1 + 1
y() = y1 + 1
}
}
}
def time(block: => Unit) = {
val beginTime=System.currentTimeMillis
block
val endTime=System.currentTimeMillis
println(endTime-beginTime)
}
time(run(1000000))
}
// scala> time(run(1000000))
// 401
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment