Locking becaue of a local lazy val?

hi internet,

i did this:
object x {
def doStuff(file:File) {
lazy val x = foo(file)

}
}

to my surprise, my profiler told me that many threads were waiting at the lazy vals lock despite it being local
why?
is the lock for the lazy val inside the object instead of the method?

Hello,

Were they waiting long? Or could it be just the normal time it takes to
obtain a lock?

 Best, Oliver

>= 2 minutes in some cases
Gesendet: Donnerstag, 04. Mai 2017 um 17:50 Uhr

Von: “Oliver Ruebenacker” <noreply@users.scala-lang.org>

An: h-star@gmx.de

Betreff: [Scala Users] [Question] Locking becaue of a local lazy val? curoli

May 4

Hello,

Were they waiting long? Or could it be just the normal time it takes to

		obtain a lock?</p>
 Best, Oliver

Visit Topic or reply to this email to respond.


In Reply To

HamsterofDeath

May 4hi internet, i did this: object x { def doStuff(file:File) { lazy val x = foo(file) … } } to my surprise, my profiler told me that many threads were waiting at the lazy vals lock despite it being local why? is the lock for the lazy val inside the object instead of the method?


Visit Topic or reply to this email to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, click here.

Maybe you have some buggy Scalac version, but I’ve compiled the following code (with Scalac 2.12.2) and decompiled it (with jad), and the output looks fine.

object LocalLazy3 {
  def doStuff() {
    lazy val x = "String"
    x.charAt(0)
  }
}

Looking at the decompilation output, it seems foo(file) will be called in a synchronized block, which synchronizes over a new object, not shared with other threads. If that’s true, you can’t have two threads waiting on the same lock. But if foo(file) waits on an IO operation, the calling thread will wait while holding a lock—maybe up to 2 minutes? And maybe it seems the thread is waiting on the lock while it’s waiting on I/O?

It looks like this was fixed in scala 2.12. However, in 2.11, scala would synchronize on the outer object for any lazy vals. Including local lazy vals.