Java 定时器 TimerTask 示例

Java java.util.Timer是一个实用类,可以用来计划在未来某个特定时间执行线程。

Java 时间表

java.util.TimerTask是一个 抽象类,它实现了可运行界面,我们需要扩展这个类来创建我们自己的 TimerTask,可以使用 java Timer 类来安排。

Java 时间表示例

java timer example, java timer, java timertask, java timertask example Java Timer class is thread safe and multiple threads can share a single Timer object without need for external synchronization. Timer class uses java.util.TaskQueue to add tasks at given regular interval and at any time there can be only one thread running the TimerTask, for example if you are creating a Timer to run every 10 seconds but single thread execution takes 20 seconds, then Timer object will keep adding tasks to the queue and as soon as one thread is finished, it will notify the queue and another thread will start executing. Java Timer class uses Object wait and notify methods to schedule the tasks. Here is a simple program for Java Timer and TimerTask example.

 1package com.journaldev.threads;
 2
 3import java.util.Date;
 4import java.util.Timer;
 5import java.util.TimerTask;
 6
 7public class MyTimerTask extends TimerTask {
 8
 9    @Override
10    public void run() {
11        System.out.println("Timer task started at:"+new Date());
12        completeTask();
13        System.out.println("Timer task finished at:"+new Date());
14    }
15
16    private void completeTask() {
17        try {
18            //assuming it takes 20 secs to complete the task
19            Thread.sleep(20000);
20        } catch (InterruptedException e) {
21            e.printStackTrace();
22        }
23    }
24
25    public static void main(String args[]){
26        TimerTask timerTask = new MyTimerTask();
27        //running timer task as daemon thread
28        Timer timer = new Timer(true);
29        timer.scheduleAtFixedRate(timerTask, 0, 10*1000);
30        System.out.println("TimerTask started");
31        //cancel after sometime
32        try {
33            Thread.sleep(120000);
34        } catch (InterruptedException e) {
35            e.printStackTrace();
36        }
37        timer.cancel();
38        System.out.println("TimerTask cancelled");
39        try {
40            Thread.sleep(30000);
41        } catch (InterruptedException e) {
42            e.printStackTrace();
43        }
44    }
45
46}

请注意,一个线程执行需要20秒,但Java Timer对象计划每10秒执行任务。

 1TimerTask started
 2Timer task started at:Wed Dec 26 19:16:39 PST 2012
 3Timer task finished at:Wed Dec 26 19:16:59 PST 2012
 4Timer task started at:Wed Dec 26 19:16:59 PST 2012
 5Timer task finished at:Wed Dec 26 19:17:19 PST 2012
 6Timer task started at:Wed Dec 26 19:17:19 PST 2012
 7Timer task finished at:Wed Dec 26 19:17:39 PST 2012
 8Timer task started at:Wed Dec 26 19:17:39 PST 2012
 9Timer task finished at:Wed Dec 26 19:17:59 PST 2012
10Timer task started at:Wed Dec 26 19:17:59 PST 2012
11Timer task finished at:Wed Dec 26 19:18:19 PST 2012
12Timer task started at:Wed Dec 26 19:18:19 PST 2012
13TimerTask cancelled
14Timer task finished at:Wed Dec 26 19:18:39 PST 2012

输出证实,如果一个任务已经执行,Timer会等待它完成,一旦完成,它将从队列中再次开始下一个任务. 爪哇语 可以创建计时器对象,以作为守护进程线程运行相关任务. 定时器_cancel()_方法用于终止定时器并丢弃任何预定的任务,但不会干扰当前执行的任务并让它完成. 如果计时器运行为 [守护线] (/ community/touris/daemon-thread- in-java) ,则该计时器会被运行为 [守护线] (/ community/touris/daemon-thread- in-java) 。 "Java Daemon Thread Example"),无论我们是否取消,一旦所有用户线程执行完毕,都会被终止. 计时器类包含几种附表()方法,用于在给定日期或某起延迟之后安排一次任务。 有几种schedleadAtFixedRate () 方法可以按一定间隔定期执行任务. 在使用Timer来调度任务时,您应该确保时间间隔比正常的线程执行还要多,否则任务队列大小会不断增长,最终任务总是执行. 这都是为了对Java Timer和Java TimerTask进行快速的盘点.

Published At
Categories with 技术
Tagged with
comments powered by Disqus