/** * An interface to represent clocks, so that they can be mocked out in unit tests. */ private[spark] trait Clock { def getTimeMillis(): Long def waitTillTime(targetTime: Long): Long } /** * A clock backed by the actual time from the OS as reported by the `System` API. */ private[spark] class SystemClock extends Clock { val minPollTime = 25L /** * @return the same time (milliseconds since the epoch) * as is reported by `System.currentTimeMillis()` */ def getTimeMillis(): Long = System.currentTimeMillis() /** * @param targetTime block until the current time is at least this value * @return current system time when wait has completed */ def waitTillTime(targetTime: Long): Long = { var currentTime = 0L currentTime = System.currentTimeMillis() var waitTime = targetTime - currentTime if (waitTime <= 0) { return currentTime } val pollTime = math.max(waitTime / 10.0, minPollTime).toLong while (true) { currentTime = System.currentTimeMillis() waitTime = targetTime - currentTime if (waitTime <= 0) { return currentTime } val sleepTime = math.min(waitTime, pollTime) Thread.sleep(sleepTime) } -1 } } private[streaming] class RecurringTimer(clock: Clock, period: Long, callback: (Long) => Unit, name: String) extends Logging { private val thread = new Thread("RecurringTimer - " + name) { // setDaemon(true) override def run() { loop } } @volatile private var prevTime = -1L @volatile private var nextTime = -1L @volatile private var stopped = false /** * Get the time when this timer will fire if it is started right now. * The time will be a multiple of this timer's period and more than * current system time. */ def getStartTime(): Long = { (math.floor(clock.getTimeMillis().toDouble / period) + 1).toLong * period } /** * Get the time when the timer will fire if it is restarted right now. * This time depends on when the timer was started the first time, and was stopped * for whatever reason. The time must be a multiple of this timer's period and * more than current time. */ def getRestartTime(originalStartTime: Long): Long = { val gap = clock.getTimeMillis() - originalStartTime (math.floor(gap.toDouble / period).toLong + 1) * period + originalStartTime } /** * Start at the given start time. */ def start(startTime: Long): Long = synchronized { nextTime = startTime thread.start() logInfo("Started timer for " + name + " at time " + nextTime) nextTime } /** * Start at the earliest time it can start based on the period. */ def start(): Long = { start(getStartTime()) } /** * Stop the timer, and return the last time the callback was made. * * @param interruptTimer True will interrupt the callback if it is in progress (not guaranteed to * give correct time in this case). False guarantees that there will be at * least one callback after `stop` has been called. */ def stop(interruptTimer: Boolean): Long = synchronized { if (!stopped) { stopped = true if (interruptTimer) { thread.interrupt() } thread.join() logInfo("Stopped timer for " + name + " after time " + prevTime) } prevTime } private def triggerActionForNextInterval(): Unit = { clock.waitTillTime(nextTime) callback(nextTime) prevTime = nextTime nextTime += period logInfo("Callback for " + name + " called at time " + prevTime) } /** * Repeatedly call the callback every interval. */ private def loop() { try { while (!stopped) { triggerActionForNextInterval() } triggerActionForNextInterval() } catch { case e: InterruptedException => } } } private[streaming] object RecurringTimer extends Logging { def main(args: Array[String]) { var lastRecurTime = 0L val period = 2000 def onRecur(time: Long) { val currentTime = System.currentTimeMillis() logInfo("" + currentTime + ": " + (currentTime - lastRecurTime)) lastRecurTime = currentTime } val timer = new RecurringTimer(new SystemClock(), period, onRecur, "Test") timer.start() // Thread.sleep(30 * 1000) // timer.stop(true) } }
相关推荐
这个东东能很方便的生成单片机定时器模块的代码,省去了大量的计算,很方便
通过IO口和定时器生成任意个数的PWM,方便移植到任意的单片机平台,非常实用
用单片机定时器产生的spwm波 仅使用两个定时器,得到比较精准的spwm波形。
本文档的主要内容详细介绍的是555定时器电路设计软件V1.2免费下载。 555定时器电路图设计软件,一个小巧的电路设计工具,它列出了555电路可实现的十几种应用电路单元,如丢失脉冲探测器、长延时定时器、压控振荡...
可以自动生成C程序,包括对定时器0,定时器1的操作,还有中断等
SparkStreaming_HBase将从Kafka收集过来的数据保存到HBase中数据来源:日志生成器。 编写一个python工程,用于产生行为日志,每运行一次,产生所设定的数量数据,使用Linux的定时器,每隔60s执行一次,行为日志保存...
用cube生成一个用定时器触发ADC1,ADC2同步采集的程序,单片机选择的是STM32L476RGT6,用定时器2进行ADC采集触发,更改定时器2的定时周期便可以更改ADC的采样周期,ADC1和ADC2使用同步规则模式,并用DMA进行数据的...
定时执行存储过程,创建job、删除job等等
spring定时器cron表达式生成
spring定时器spring定时器spring定时器spring定时器spring定时器
辅助软件 定时器计算器辅助软件 定时器计算器辅助软件 定时器计算器辅助软件 定时器计算器辅助软件 定时器计算器辅助软件 定时器计算器辅助软件 定时器计算器辅助软件 定时器计算器辅助软件 定时器计算器辅助软件 ...
555定时器 555定时器 555定时器 555定时器
如果先初始化从模式定时器3,定时器3工作在触发模式下,TRGI信号来源为定时器2的TRGO,而定时器2还未初始化,故TRGO信号来源默认是UG位,故在定时器2初始化过程中会置位UG位,从而触发定时器3的从模式触发模式,导致...
单片机中断 定时器 欢迎大家的使用 中断系统是一个很重要的概念
S7-1200的定时器为IEC定时器,用户程序中可以使用的定时器数量...生成脉冲定时器(TP) 接通延时定时器(TON) 关断延时定时器(TOF) 时间累加器(TONR) 此外还包含复位定时器(RT)和加载持续时间(PT)这两个指令。
STM32F103单片机高级定时器TIM1 从PA8 ,PA8,PA10,PA11,同时生成4路PWM.库函数版。
555集成定时器 555集成定时器 555集成定时器
js代码-有限次数定时器生成器
VC++ 定时器使用 定时器使用 VC++ 定时器使用 定时器使用