ScheduledExecutorService 中兩個方法

這個是用來執行計劃任務的。
有兩個方法

  public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);


public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);

第一個方法scheduleAtFixedRate 以固定的頻率執行,首先會延遲 initialDelay開始執行。然後下一次執行,距離上一次開始的執行的時間間隔爲 period。 period是計算兩個任務開始執行的時間的間隔的。
這裏寫圖片描述

此時會存在一個情況,就是這個任務執行的時間超過了,period時間,那麼下一個任務將會在任務執行完畢後啓動。
這裏寫圖片描述

scheduleWithFixedDelay ,它也是首先延遲initialDelay開始執行。然後下一次執行,是距離上一次執行完畢的時間間隔爲delay。 delay是計算上一個任務執行完畢的時間和本次任務開始時間的差值,此值和任務的執行時間就沒有關係了。

這裏寫圖片描述

這裏寫圖片描述

下面是代碼:

initialDelay 爲1s ,delay 爲5s 。 任務執行時間 爲 1s 或者6s。

public class TestNewScheduledThreadPool {

    public static void main(String args[]) {
        ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
        System.out.println("sheduleAtFixedRate  " + DateFormat.getTimeInstance().format(new Date()));
        //sheduleAtFixedRate(service, 1000);
        //sheduleAtFixedRate(service, 6000);

          scheduleWithFixedDelay(service, 1000);
        //scheduleWithFixedDelay(service, 6000);

    }

    private static void sheduleAtFixedRate(ScheduledExecutorService service, final int sleepTime) {
        service.scheduleAtFixedRate(new Runnable() {
            public void run() {

                long start = new Date().getTime();
                System.out.println("sheduleAtFixedRate 開始執行時間 " + DateFormat.getTimeInstance().format(new Date()));
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // long end = System.currentTimeMillis();
                long end = new Date().getTime();
                System.out.println("scheduleAtFixedRate 執行花費時間= " + (end - start) / 1000);
                System.out.println(">>>>>>>>>>>>>>>>>>>");
            }

        }, 1000, 5000, TimeUnit.MILLISECONDS);
    }

    private static void scheduleWithFixedDelay(ScheduledExecutorService service, final int sleepTime) {
        service.scheduleWithFixedDelay(new Runnable() {

            public void run() {
                long start = new Date().getTime();
                System.out.println("scheduleWithFixedDelay 開始執行時間:" + DateFormat.getTimeInstance().format(new Date()));
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long end = new Date().getTime();
                System.out.println("scheduleWithFixedDelay執行花費時間=" + (end - start) / 1000 + "m");
                System.out.println("scheduleWithFixedDelay執行完成時間:" + DateFormat.getTimeInstance().format(new Date()));
                System.out.println("======================================");
            }
        }, 1000, 5000, TimeUnit.MILLISECONDS);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章