TimerTask 使用记录
程序员文章站
2022-06-27 21:10:47
1、调用者使用MyTimerTask.instance(120)?.start(object : MyTimerTask.OnTimerTaskUI { override fun onUI(time: Int) { } })2、被调用类/** * Created by zxy on 2020/7/21 14:17 * ****************************************** * * * *...
1、调用者使用
MyTimerTask.instance(120)?.start(object : MyTimerTask.OnTimerTaskUI {
override fun onUI(time: Int) {
}
})
2、被调用类
/**
* Created by zxy on 2020/7/21 14:17
* ******************************************
* *
* ******************************************
*/
class MyTimerTask : TimerTask {
private var maxTime = 0
private var time = -1
val mTimer = Timer()
private lateinit var onTimerTaskUI:OnTimerTaskUI
interface OnTimerTaskUI{
fun onUI(time:Int)
}
//zxy 单例模式
private constructor(maxTime: Int) {
this.maxTime = maxTime
}
companion object {
@Volatile
private var instance: MyTimerTask? = null
fun instance(maxTime: Int): MyTimerTask? {
if (instance == null) {
synchronized(MyTimerTask::class.java) {
if (instance == null) {
instance = MyTimerTask(maxTime)
}
}
}
return instance
}
}
override fun run() {
time-=1
onTimerTaskUI.onUI(time)
}
/**
* 开始
*/
fun start(onTimerTaskUI:OnTimerTaskUI,step: Long = 1000) {
this.onTimerTaskUI =onTimerTaskUI
mTimer.schedule(this, 0, step)
}
}
本文地址:https://blog.csdn.net/fayangzhou/article/details/107488289
上一篇: Node.js 回调函数(阻塞与非阻塞)
下一篇: Skywalking使用体验