1
Kotlin에 Dead man's switch을 구현하고 싶습니다. 이는 마지막으로 MyEvent
을받은 후 TIME_INTERVAL
초 후에 알림을 발생시킵니다. 새로운 MyEvent
이 수신되면 타이머가 다시 시작됩니다. Kotlin의 죽은 사람의 스위치
private val stopWatch = object :() -> Unit {
var timer = System.currentTimeMillis()
var isRunning = false
override fun invoke() {
timer = System.currentTimeMillis()
if (isRunning) return
synchronized(this) {
isRunning = true
while (System.currentTimeMillis() - timer <= TIME_INTERVAL) {}
fireNotification()
isRunning = false
}
}
}
override fun onSomeEvent(e: MyEvent?) {
runAsync(stopWatch)
}
는 어느
kotlin.concurrent
또는 Java 표준 라이브러리를 사용하여이 기능을 얻을 수있는 간단하거나 쉬운 방법이 있습니까?
감사합니다! 빈 루프는 예제에 불과합니다. 나는 쉽게 잘 수있다. 예 : 'while (System.currentTimeMillis() - timer <= 2000) {Thread.sleep (Math.abs (2000 - (System.currentTimeMillis() - timer))}'. 또한'private var future : ScheduledFuture? = null;'나는 오히려 피해야 할 패턴입니다. –
breandan