我有这样一个场景,来自传感器的数据每5分钟发送到服务器。服务器将接收到的数据存储在数据库中。现在,当服务器接收到数据时,我想为这个特定的传感器启动6分钟计时器。如果我在这6分钟之前收到数据,必须取消并重新启动计时器。如果计时器刚好结束,
onFinish()
必须调用,然后我将向用户发送关于传感器可能存在连接问题的通知。我有一个传感器列表和一个在服务器接收数据时调用的方法:
// list to save sensors and their timers
private val sensors: MutableMap<Long, CountDownTimer> = HashMap()
// this method gets called after server receives data from the sensor
// must keep in mind that CountDownTimer is undefined
fun initiateTimer(sensorId: Long) {
sensors[sensorId] = object : CountDownTimer(360000, 1000) {
fun onTick(duration: Long) {
// Blank
}
fun onFinish() {
// Send notification to user about possible connection issue
}
}.start()
}
我一直在调查
scheduleWithFixedDelay
,但这需要
ExecutorService
使用特定线程计数。现在,如果我有数千个传感器,而我需要同时运行这数千个计时器,该怎么办?
现在在安卓系统中,我可以简单地使用
CountDownTimer
,但由于它不是安卓系统,我正在寻求使用
Kotlin
在里面
Spring Boot
环境(或者简单地说,不是
Android
环境)。非常感谢。