Provide lazy implementation with an external object to synchronize on.

This commit is contained in:
Ilya Gorbunov
2015-09-01 18:46:27 +03:00
parent 4fbf787f7d
commit b3073dbd2d
5 changed files with 46 additions and 3 deletions
@@ -17,6 +17,23 @@ class LazyValTest {
}
}
class SynchronizedLazyValTest {
volatile var result = 0
val a by lazy(this) {
++result
}
test fun doTest() {
synchronized(this) {
// thread { a } // not available in js // TODO: Make this test JVM-only
result = 1
a
}
assertTrue(a == 2, "fail: initializer should be invoked only once")
assertTrue(result == 2, "fail result should be incremented after test")
}
}
class UnsafeLazyValTest {
var result = 0
val a by lazy(LazyThreadSafetyMode.NONE) {