feiyu02
2022-06-28 5670e4a15fba292ef5f8fb90e96072de976bb621
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package cn.flightfeather.supervision.timingtask
 
import java.time.LocalDateTime
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
 
/**
 * 定时任务基类
 */
abstract class BaseTimingTask {
 
    companion object {
        var threadPoo: ExecutorService? = Executors.newCachedThreadPool()
    }
 
    // 记录上次任务执行的时间点,单位:毫秒
    private var lastTime: LocalDateTime = LocalDateTime.MIN
 
    // 任务执行周期,单位:分钟
    abstract val period: Long
 
    fun execute(localtime:LocalDateTime) {
        val now = LocalDateTime.now()
        if (now.minusSeconds(period * 60 - 5) >= lastTime) {
            lastTime = now
            doTask(localtime)
        }
    }
 
    abstract fun doTask(localtime:LocalDateTime)
}