althaf
10/11/2021, 8:57 AMclass CurrentServerTimestamp {
private val lock: ReentrantLock = ReentrantLock()
private var timestampInMillis: Long
get() = lock.withLock { timestampInMillis }
set(value) {
lock.withLock { timestampInMillis = value }
}
init {
timestampInMillis = CURRENT_SERVER_TIME_NOT_SET
}
companion object {
private val instance = CurrentServerTimestamp()
fun isValidTimeStamp(): Boolean {
return instance.timestampInMillis != CURRENT_SERVER_TIME_NOT_SET
}
fun get(): Long = instance.timestampInMillis
fun set(currentServerTimestamp: Long) {
instance.timestampInMillis = currentServerTimestamp
}
}
}
set(value) {
lock.withLock { timestampInMillis = value }
}
I think this is causing the dead lock, is there any way to over come this and still i get the intended behavior ?ephemient
10/11/2021, 9:02 AMprintln
in there). if you want to access the backing field, do so explicitly:
private var timestampInMillis: Long
get() = lock.withLock { field }
set(value) {
lock.withLock { field = value }
}
ephemient
10/11/2021, 9:03 AMprivate var timestampInMillis: Long = CURRENT_SERVER_TIME_NOT_SET
get() = ...
set(value) { ... }
that sets the backing field directly, not going through the custom accessor, but that should be fine for this case (and most cases)althaf
10/11/2021, 9:18 AMalthaf
10/11/2021, 9:18 AM