Hey, is there a better way to create properties where getters and setters are protected using a lock?
private val lock = ReentrantReadWriteLock()
fun getName(): String {
lock.readLock().lock()
try {
return name
} finally {
lock.readLock().unlock()
}
}
d
diesieben07
06/20/2017, 9:22 PM
jschneider: First of all, your
fun getName
should really be a
val name
with a getter. And then check out the extension functions on
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.*
class K {
private val lock = ReentrantReadWriteLock()
var name = "initial"
get() = lock.read { field }
set(new) = lock.write { field = new }
}