ScheduleExecutorService接口
int poolSize = 500;//定義線程調度池 ScheduledExecutorService execSrv = Executors.newScheduledThreadPool(poolSize); //啓用線程調度 ChildThread childThread = new AlarmChildThread(1, ruleList); int period = 30;//調用週期 long initialDelay = 60;//延遲時間 execSrv.scheduleAtFixedRate(childThread, initialDelay, period * 60000, TimeUnit.MILLISECONDS);
scheduleAtFixedRate有兩個時間參數,initialDelay和period,對應該方法的兩個主要功能,即延遲運行任務和週期性執行任務。
childThread:執行線程
initialDelay:初始化延時
period:兩次開始執行最小間隔時間
unit:計時單位
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
command:執行線程
initialDelay:初始化延時
period:前一次執行結束到下一次執行開始的間隔時間(間隔執行延遲時間)
unit:計時單位