該接口在java.util.concurrent包下,具體可參見API
項目中用到了cheduledExecutorService中的scheduleWithFixedDelay方法,就順便把其中幾個關重要的方法學習下:
1.schedule
schedule(Runnable command, long delay, TimeUnit unit),schedule方法被用來延遲指定時間後執行某個指定任務。
command:定時任務
delay:延遲時間
unit:時間單位
a.代碼如下:
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author someone
* @create 2017-10-10 19:01
**/
public class Job implements Runnable {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("do something at:" + sdf.format(new Date()));
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author someone
* @create 2017-10-10 19:01
**/
public class ScheduledExecutorServiceTest {
//測試
public static void main(String[] args) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(" begin to do something at:" + sdf.format(new Date()));
schedule.schedule(new Job(),1, TimeUnit.SECONDS);
}
}
b.輸出如下:
begin to do something at:2017-10-10 07:02:31
do something at:2017-10-10 07:02:37
2.scheduleWithFixedDelay
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit)
創建並執行一個在給定初始延遲後首次啓用的定期操作,隨後,在每一次執行終止和下一次執行開始之間都存在給定的延遲,如果任務的執行時間超過了廷遲時間(delay),下一個任務則會在
(當前任務執行所需時間+delay)後執行。
command:定時任務
initialDelay:初始延遲時間
delay:指定延遲時間
unit:時間單位
a.代碼如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author someone
* @create 2017-10-10 19:01
**/
public class ScheduledExecutorServiceTest {
public static void main(String[] args) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(" begin to do something at:" + sdf.format(new Date()));
//schedule.schedule(new Job(),1, TimeUnit.SECONDS);
schedule.scheduleWithFixedDelay(new Job(), 1, 2, TimeUnit.SECONDS);
}
}
b.輸出如下:
begin to do something at:2017-10-10 07:06:51
do something at:2017-10-10 07:06:57
do something at:2017-10-10 07:07:04
do something at:2017-10-10 07:07:11
do something at:2017-10-10 07:07:18
do something at:2017-10-10 07:07:25
第一次延時5+1s
以後每次延時5+2s
3.scheduleAtFixedRate
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
創建並執行一個在給定初始延遲後首次啓用的定期操作,後續操作具有給定的週期;也就是將在 initialDelay 後開始執行,然後在initialDelay+period 後執行,接着在 initialDelay + 2 * period 後執行,依此類推。如果任務的執行時間小於period,將會按上述規律執行。否則,則會按 任務的實際執行時間進行週期執行。
initialDelay:初始延時時間
period:延時週期
unit:時間單位
a.代碼如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @author someone
* @create 2017-10-10 19:01
**/
public class ScheduledExecutorServiceTest {
public static void main(String[] args) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(" begin to do something at:" + sdf.format(new Date()));
//schedule.schedule(new Job(),1, TimeUnit.SECONDS);
//schedule.scheduleWithFixedDelay(new Job(), 1, 2, TimeUnit.SECONDS);
schedule.scheduleAtFixedRate(new Job(), 1,2, TimeUnit.SECONDS);
}
}
b.結果輸出:
begin to do something at:2017-10-10 07:13:57
do something at:2017-10-10 07:14:03
do something at:2017-10-10 07:14:08
do something at:2017-10-10 07:14:13
do something at:2017-10-10 07:14:18
do something at:2017-10-10 07:14:23
初始時間延時:5+1s
以後每次延時爲1+2*2s