/**
* ScheduledExecutorService是从Java SE5的java.util.concurrent里,做为并发工具类被引进的,这是最理想的定时任务实现方式。
* 它有以下好处:
* 1>相比于Timer的单线程,它是通过线程池的方式来执行任务的
* 2>可以很灵活的去设定第一次执行任务的延迟时间
* 3>提供了良好的约定,以便设定执行的时间间隔
* 下面是实现代码,我们通过ScheduledExecutorService#scheduleAtFixedRate展示这个例子,通过代码里参数的控制,首次执行加了延迟时间。
*/
object pr extends Logging {
def main(args: Array[String]): Unit = {
val runnable = new Runnable {
override def run() = {
println("Hello !!")
}
}
val service = Executors.newSingleThreadScheduledExecutor()
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
service.scheduleAtFixedRate(runnable, 10, 1, TimeUnit.SECONDS)
}
}