I'm going to measure performance of some code. For the purpose, I introduced the following method:
def timed[T](body: => T) = {
val start = System.currentTimeMillis
blackHole = body
val end = System.currentTimeMillis
end - start
}
The blackHole
variable introduced in order to preserve body
result. This is done to prevent dead code elimination by JVM.
In some books there is a statement that blackHole
should be declared as follows:
@volatile
var blackHole: Any = _
Why should one mark the variable volatile
?