ScheduledThreadPool構造方法
public static ScheduleExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduleThreadPoolExecutor(corePoolSize);
}
}
複製程式碼
特點: 延遲執行
使用案例:
val pool: ScheduleExecutorService = Executors.newScheduledThreadPool()
複製程式碼
txt.click {
val runnable = Runnable {
try {
Thread.sleep(2000)
log("我是延遲執行的${System.currentTime}")
}catch(e: Exception) {
e.printStackTrace()
}
}
pool.schedule(runnable, 10, TimeUnit.SECONDS)
}
//延遲5秒啟動,每1秒執行一次
pool.schaduleAtFixedRate(runnable, 5, 1, TimeUnit.SECONDS)
//啟動後第一次延遲5秒執行,後面延遲1秒執行
pool.schaduleWithFixedDelay(runnable, 5, 1, TimeUnit.SECONDS)
複製程式碼