歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java定時器Timer簡述

概述

主要用於Java線程裡指定時間或周期運行任務。Timer是線程安全的,但不提供實時性(real-time)保證。

構造函數

Timer()

默認構造函數。

Timer(boolean)

指定關聯線程是否作為daemon線程。

Timer(String)

指定關聯線程的名稱。

Timer(String, boolean)

同時指定關聯線程的名稱和是否作為daemon。

schdule方法

schedule(TimerTask task, long delay)

以當前時間為基准,延遲指定的毫秒後執行一次TimerTask任務。

schedule(TimerTask task, Date time)

在指定的日期執行一次TimerTask任務。

如果日期time早於當前時間,則立刻執行。

使用示例

public class Demo {
    private static Timer timer = new Timer();

    public static class MyTask extends TimerTask {
        @Override
        publicvoidrun() {
            System.out.println("Run Time:" + new Date().toString());
        }
    }
    
    publicstaticvoidmain(String[] args) {
        try {
            MyTask task = new MyTask();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = "2016-12-27 14:36:00";
            Date date = sdf.parse(dateStr);
            System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString());
            timer.schedule(task, date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

執行結果

Date = Tue Dec 27 14:36:00 CST 2016 NowTime = Tue Dec 27 21:28:04 CST 2016
Run Time:Tue Dec 27 21:28:04 CST 2016

說明是立刻執行。

schedule(TimerTask task, long delay, long period)

以當前時間為基准,延遲指定的毫秒後,再按指定的時間間隔地無限次數的執行TimerTask任務。(fixed-delay execution)

使用示例

public class Demo {
    private static Timer timer = new Timer();

    public static class MyTask extends TimerTask {
        @Override
        publicvoidrun() {
            System.out.println("Run Time: " + new Date().toString());
        }
    }

    publicstaticvoidmain(String[] args) {
        MyTask task = new MyTask();
        System.out.println("Now Time: " + new Date().toString());
        timer.schedule(task, 3000, 5000);
    }
}

執行結果

Now Time: Tue Dec 27 21:34:59 CST 2016
Run Time: Tue Dec 27 21:35:02 CST 2016
Run Time: Tue Dec 27 21:35:07 CST 2016
Run Time: Tue Dec 27 21:35:12 CST 2016
Run Time: Tue Dec 27 21:35:17 CST 2016

說明以當前基准時間延遲3秒後執行一次,以後按指定間隔時間5秒無限次數的執行。

schedule(TimerTask task, Date firstTime, long period)

在指定的日期之後,按指定的時間間隔地無限次數的執行TimerTask任務。(fixed-delay execution)

如果日期firstTime早於當前時間,則立刻執行,且不執行在時間差內的任務。

使用示例

public class Demo {
    private static Timer timer = new Timer();

    public static class MyTask extends TimerTask {
        @Override
        publicvoidrun() {
            System.out.println("Run Time:" + new Date().toString());
        }

        publicstaticvoidmain(String[] args) {
            try {
                MyTask task = new MyTask();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateStr = "2016-12-27 14:36:00";
                Date date = sdf.parse(dateStr);
                System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString());
                timer.schedule(task, date, 3000);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}

執行結果

Date = Tue Dec 27 14:36:00 CST 2016 NowTime = Tue Dec 27 21:43:30 CST 2016
Run Time:Tue Dec 27 21:43:30 CST 2016
Run Time:Tue Dec 27 21:43:33 CST 2016
Run Time:Tue Dec 27 21:43:36 CST 2016

說明指定的之間早於當前時間,則立刻執行,不會補充時間差內的任務

scheduleAtFixedRate方法

scheduleAtFixedRate(TimerTask task, long delay, long period)

以當前時間為基准,延遲指定的毫秒後,再按指定的時間間隔周期性地無限次數的執行TimerTask任務。(fixed-rate execution)

使用示例

public class Demo {
    private static Timer timer = new Timer();

    public static class MyTask extends TimerTask {
        @Override
        publicvoidrun() {
            System.out.println("Run Time: " + new Date().toString());
        }
    }

    publicstaticvoidmain(String[] args) {
        MyTask task = new MyTask();
        System.out.println("Now Time: " + new Date().toString());
        timer.scheduleAtFixedRate(task, 3000, 5000);
    }
}

執行結果

Now Time: Tue Dec 27 21:58:03 CST 2016
Run Time: Tue Dec 27 21:58:06 CST 2016
Run Time: Tue Dec 27 21:58:11 CST 2016
Run Time: Tue Dec 27 21:58:16 CST 2016
Run Time: Tue Dec 27 21:58:21 CST 2016

說明以當前基准時間延遲3秒後執行一次,以後按指定間隔時間5秒無限次數的執行。

scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

在指定的日期之後,按指定的時間間隔周期性地無限次數的執行TimerTask任務。(fixed-rate execution)

如果日期firstTime早於當前時間,則立即執行,並補充性的執行在時間差內的任務。

使用示例

public class Demo {
    private static Timer timer = new Timer();

    public static class MyTask extends TimerTask {
        @Override
        publicvoidrun() {
            System.out.println("Run Time:" + new Date().toString());
        }

        publicstaticvoidmain(String[] args) {
            try {
                MyTask task = new MyTask();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateStr = "2016-12-27 22:02:00";
                Date date = sdf.parse(dateStr);
                System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString());
                timer.scheduleAtFixedRate(task, date, 5000);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}

執行結果

Date = Tue Dec 27 22:02:00 CST 2016 NowTime = Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:54 CST 2016
Run Time:Tue Dec 27 22:02:55 CST 2016
Run Time:Tue Dec 27 22:03:00 CST 2016
Run Time:Tue Dec 27 22:03:05 CST 2016

說明指定的之間早於當前時間,則立刻執行。

在時間22:02:00--22:02:54內大約有11個5秒間隔,則優先補充性的執行在時間差內的任務,然後在22:02:55補充完畢(執行12次。ps:0-55秒區間段內首位都算上,正好觸發12次),此後每隔5秒執行一次定時任務。

執行任務延時對比之 schedule 和 scheduleAtFixedRate

schedule不延時

使用示例

public class Demo {
    private static Timer timer = new Timer();
    private static int runCount = 0;

    public static class MyTask extends TimerTask {
        @Override
        publicvoidrun() {
            try {
                System.out.println("Begin Run Time: " + new Date().toString());
                Thread.sleep(3000);
                System.out.println("End Run Time: " + new Date().toString());
                runCount++;
                if (runCount == 3) {
                    timer.cancel();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    publicstaticvoidmain(String[] args) {
        try {
            MyTask task = new MyTask();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = "2016-12-27 14:36:00";
            Date date = sdf.parse(dateStr);
            System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString());
            timer.schedule(task, date, 5000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

執行結果

早於當前基准時間

Date = Tue Dec 27 14:36:00 CST 2016 NowTime = Tue Dec 27 22:23:37 CST 2016
Begin Run Time: Tue Dec 27 22:23:37 CST 2016
End Run Time: Tue Dec 27 22:23:40 CST 2016
Begin Run Time: Tue Dec 27 22:23:42 CST 2016
End Run Time: Tue Dec 27 22:23:45 CST 2016
Begin Run Time: Tue Dec 27 22:23:47 CST 2016
End Run Time: Tue Dec 27 22:23:50 CST 2016

Process finished with exit code 0

晚於當前基准時間

Date = Tue Dec 27 22:42:00 CST 2016 NowTime = Tue Dec 27 22:41:54 CST 2016
Begin Run Time: Tue Dec 27 22:42:00 CST 2016
End Run Time: Tue Dec 27 22:42:03 CST 2016
Begin Run Time: Tue Dec 27 22:42:05 CST 2016
End Run Time: Tue Dec 27 22:42:08 CST 2016
Begin Run Time: Tue Dec 27 22:42:10 CST 2016
End Run Time: Tue Dec 27 22:42:13 CST 2016

Process finished with exit code 0

不管早還是晚於基准時間,都不進行補償,下一次任務的執行時間參考的是上一次任務的開始時間點來計算。

schedule延時

使用示例

public class Demo {
    private static Timer timer = new Timer();
    private static int runCount = 0;

    public static class MyTask extends TimerTask {
        @Override
        publicvoidrun() {
            try {
                System.out.println("Begin Run Time: " + new Date().toString());
                Thread.sleep(5000);
                System.out.println("End Run Time: " + new Date().toString());
                runCount++;
                if (runCount == 3) {
                    timer.cancel();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    publicstaticvoidmain(String[] args) {
        try {
            MyTask task = new MyTask();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = "2016-12-27 22:42:00";
            Date date = sdf.parse(dateStr);
            System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString());
            timer.schedule(task, date, 3000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

執行結果

早於當前基准時間

Date = Tue Dec 27 22:42:00 CST 2016 NowTime = Tue Dec 27 22:45:17 CST 2016
Begin Run Time: Tue Dec 27 22:45:17 CST 2016
End Run Time: Tue Dec 27 22:45:22 CST 2016
Begin Run Time: Tue Dec 27 22:45:22 CST 2016
End Run Time: Tue Dec 27 22:45:27 CST 2016
Begin Run Time: Tue Dec 27 22:45:27 CST 2016
End Run Time: Tue Dec 27 22:45:32 CST 2016

Process finished with exit code 0

晚於當前基准時間

Date = Tue Dec 27 22:47:00 CST 2016 NowTime = Tue Dec 27 22:46:27 CST 2016
Begin Run Time: Tue Dec 27 22:47:00 CST 2016
End Run Time: Tue Dec 27 22:47:05 CST 2016
Begin Run Time: Tue Dec 27 22:47:05 CST 2016
End Run Time: Tue Dec 27 22:47:10 CST 2016
Begin Run Time: Tue Dec 27 22:47:10 CST 2016
End Run Time: Tue Dec 27 22:47:15 CST 2016

Process finished with exit code 0

不管早還是晚於當前基准時間,都不進行補償,下一次任務的執行時間都是參考上一次任務結束的時間點來計算。

scheduleAtFixedRate不延時

使用示例

public class Demo {
    private static Timer timer = new Timer();
    private static int runCount = 0;

    public static class MyTask extends TimerTask {
        @Override
        publicvoidrun() {
            try {
                System.out.println("Begin Run Time: " + new Date().toString());
                Thread.sleep(3000);
                System.out.println("End Run Time: " + new Date().toString());
                runCount++;
                if (runCount == 1000) {
                    timer.cancel();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    publicstaticvoidmain(String[] args) {
        try {
            MyTask task = new MyTask();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = "2016-12-27 22:51:42";
            Date date = sdf.parse(dateStr);
            System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString());
            timer.scheduleAtFixedRate(task, date, 5000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

執行結果

早於當前基准時間

Date = Tue Dec 27 22:51:42 CST 2016 NowTime = Tue Dec 27 22:51:57 CST 2016
Begin Run Time: Tue Dec 27 22:51:57 CST 2016
End Run Time: Tue Dec 27 22:52:00 CST 2016
Begin Run Time: Tue Dec 27 22:52:00 CST 2016
End Run Time: Tue Dec 27 22:52:03 CST 2016
Begin Run Time: Tue Dec 27 22:52:03 CST 2016
End Run Time: Tue Dec 27 22:52:06 CST 2016
Begin Run Time: Tue Dec 27 22:52:06 CST 2016
End Run Time: Tue Dec 27 22:52:09 CST 2016
Begin Run Time: Tue Dec 27 22:52:09 CST 2016
End Run Time: Tue Dec 27 22:52:12 CST 2016
Begin Run Time: Tue Dec 27 22:52:12 CST 2016
End Run Time: Tue Dec 27 22:52:15 CST 2016
Begin Run Time: Tue Dec 27 22:52:15 CST 2016
End Run Time: Tue Dec 27 22:52:18 CST 2016
Begin Run Time: Tue Dec 27 22:52:18 CST 2016
End Run Time: Tue Dec 27 22:52:21 CST 2016
Begin Run Time: Tue Dec 27 22:52:22 CST 2016
End Run Time: Tue Dec 27 22:52:25 CST 2016
Begin Run Time: Tue Dec 27 22:52:27 CST 2016
End Run Time: Tue Dec 27 22:52:30 CST 2016
Begin Run Time: Tue Dec 27 22:52:32 CST 2016
End Run Time: Tue Dec 27 22:52:35 CST 2016
Begin Run Time: Tue Dec 27 22:52:37 CST 2016
End Run Time: Tue Dec 27 22:52:40 CST 2016
Begin Run Time: Tue Dec 27 22:52:42 CST 2016
End Run Time: Tue Dec 27 22:52:45 CST 2016
Begin Run Time: Tue Dec 27 22:52:47 CST 2016
End Run Time: Tue Dec 27 22:52:50 CST 2016
Begin Run Time: Tue Dec 27 22:52:52 CST 2016
End Run Time: Tue Dec 27 22:52:55 CST 2016
Begin Run Time: Tue Dec 27 22:52:57 CST 2016
End Run Time: Tue Dec 27 22:53:00 CST 2016

Process finished with exit code 0

晚於當前基准時間

Date = Tue Dec 27 22:37:00 CST 2016 NowTime = Tue Dec 27 22:36:06 CST 2016
Begin Run Time: Tue Dec 27 22:37:00 CST 2016
End Run Time: Tue Dec 27 22:37:03 CST 2016
Begin Run Time: Tue Dec 27 22:37:05 CST 2016
End Run Time: Tue Dec 27 22:37:08 CST 2016
Begin Run Time: Tue Dec 27 22:37:10 CST 2016
End Run Time: Tue Dec 27 22:37:13 CST 2016

Process finished with exit code 0

不延時的情況下,當早於基准時間時,時間差內的執行任務未補償完時,下一次執行任務的時間參考的是上一次執行任務的結束時間;一旦補償完畢(注意粗體時間點),下一次執行任務的時間參考的是上一次執行任務的開始時間;當晚於基准時間時,下一次執行任務的時間參考的是上一次執行任務的開始時間。

scheduleAtFixedRate延時

使用示例

public class Demo {
    private static Timer timer = new Timer();
    private static int runCount = 0;

    public static class MyTask extends TimerTask {
        @Override
        publicvoidrun() {
            try {
                System.out.println("Begin Run Time: " + new Date().toString());
                Thread.sleep(5000);
                System.out.println("End Run Time: " + new Date().toString());
                runCount++;
                if (runCount == 3) {
                    timer.cancel();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    publicstaticvoidmain(String[] args) {
        try {
            MyTask task = new MyTask();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = "2016-12-27 22:28:00";
            Date date = sdf.parse(dateStr);
            System.out.println("Date = " + date.toString() + " NowTime = " + new Date().toString());
            timer.scheduleAtFixedRate(task, date, 3000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

執行結果

早於當前基准時間

Date = Tue Dec 27 23:01:00 CST 2016 NowTime = Tue Dec 27 23:01:19 CST 2016
Begin Run Time: Tue Dec 27 23:01:19 CST 2016
End Run Time: Tue Dec 27 23:01:24 CST 2016
Begin Run Time: Tue Dec 27 23:01:24 CST 2016
End Run Time: Tue Dec 27 23:01:29 CST 2016
Begin Run Time: Tue Dec 27 23:01:29 CST 2016
End Run Time: Tue Dec 27 23:01:34 CST 2016
Begin Run Time: Tue Dec 27 23:01:34 CST 2016
End Run Time: Tue Dec 27 23:01:39 CST 2016

晚於當前基准時間

Date = Tue Dec 27 22:28:00 CST 2016 NowTime = Tue Dec 27 22:27:55 CST 2016
Begin Run Time: Tue Dec 27 22:28:00 CST 2016
End Run Time: Tue Dec 27 22:28:05 CST 2016
Begin Run Time: Tue Dec 27 22:28:05 CST 2016
End Run Time: Tue Dec 27 22:28:10 CST 2016
Begin Run Time: Tue Dec 27 22:28:10 CST 2016
End Run Time: Tue Dec 27 22:28:15 CST 2016

Process finished with exit code 0

延時的情況下,即使是早於基准時間,由於延時效應,根本不可能補償完畢時間差內的執行任務,故而在延時的情況下,下一次任務的執行時間都是參考上一次任務結束的時間來計算。

對比總結

 執行任務不延時執行任務延時 早於當前基准時間 schedule:下一次任務的執行時間參考的是上一次任務的開始時間來計算。 scheduleAtFixedRate:當早於基准時間時,時間差內的執行任務未補償完時,下一次執行任務的時間參考的是上一次任務的結束時間;一旦補償完畢,下一次執行任務的時間參考上一次任務的開始時間來計算。 二者一樣。下一次任務的執行時間都是參考上一次任務的結束時間來計算。 晚於當前基准時間 二者一樣。下一次任務的執行時間參考的是上一次任務的開始時間來計算。 二者一樣。下一次任務的執行時間都是參考上一次任務的結束時間來計算。

注意:scheduleAtFixedRate示例中firstTime是有區別的,而結果證明是有補償性操作的。


(END)

Copyright © Linux教程網 All Rights Reserved