Skip to content

Commit 94cfca1

Browse files
committed
Don't make the lock global, only for the instance of the field
1 parent 81c347d commit 94cfca1

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/main/kotlin/com/demonwav/mcdev/util/delegates.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ package com.demonwav.mcdev.util
1212

1313
import kotlin.reflect.KProperty
1414

15-
private object Lock
16-
1715
class NullableDelegate<T>(private val supplier: () -> T?) {
1816
private var field: T? = null
1917

18+
private val lock = Lock()
19+
2020
operator fun getValue(thisRef: Any?, property: KProperty<*>): T? {
2121
if (field != null) {
2222
return field
2323
}
2424

25-
synchronized(Lock) {
25+
synchronized(lock) {
2626
if (field != null) {
2727
return field
2828
}
@@ -36,6 +36,8 @@ class NullableDelegate<T>(private val supplier: () -> T?) {
3636
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
3737
this.field = value
3838
}
39+
40+
private inner class Lock
3941
}
4042

4143
fun <T> nullable(supplier: () -> T?): NullableDelegate<T> = NullableDelegate(supplier)

0 commit comments

Comments
 (0)