【Java多线程】之十:Timer and TimerTask
来源:互联网 发布:软件脱壳 编辑:程序博客网 时间:2024/04/29 00:07
java.util.Timer is a utility class that can be used to schedule a thread to be executed at certain time in future. Java Timer class can be used to schedule a task to be run one-time or to be run at regular intervals.
java.util.TimerTask is an abstract class that implements Runnable interface and we need to extend this class to create our own TimerTask that can be scheduled using java Timer class.
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.
Timer class uses Object wait and notify methods to schedule the tasks.
Here is an example of Timer and TimerTask implementation.
MyTimerTask.java
package com.journaldev.threads;import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class MyTimerTask extends TimerTask { @Override public void run() { System.out.println("Timer task started at:"+new Date()); completeTask(); System.out.println("Timer task finished at:"+new Date()); } private void completeTask() { try { //assuming it takes 20 secs to complete the task Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String args[]){ TimerTask timerTask = new MyTimerTask(); //running timer task as daemon thread Timer timer = new Timer(true); timer.scheduleAtFixedRate(timerTask, 0, 10*1000); System.out.println("TimerTask started"); //cancel after sometime try { Thread.sleep(120000); } catch (InterruptedException e) { e.printStackTrace(); } timer.cancel(); System.out.println("TimerTask cancelled"); try { Thread.sleep(30000); } catch (InterruptedException e) { e.printStackTrace(); } }}
Notice that one thread execution will take 20 seconds but Timer object is scheduled to run the task every 10 seconds. Here is the output of the program:
TimerTask startedTimer task started at:Sun Aug 09 16:27:28 CST 2015Timer task finished at:Sun Aug 09 16:27:48 CST 2015Timer task started at:Sun Aug 09 16:27:48 CST 2015Timer task finished at:Sun Aug 09 16:28:08 CST 2015Timer task started at:Sun Aug 09 16:28:08 CST 2015Timer task finished at:Sun Aug 09 16:28:28 CST 2015Timer task started at:Sun Aug 09 16:28:28 CST 2015Timer task finished at:Sun Aug 09 16:28:48 CST 2015Timer task started at:Sun Aug 09 16:28:48 CST 2015Timer task finished at:Sun Aug 09 16:29:08 CST 2015Timer task started at:Sun Aug 09 16:29:08 CST 2015TimerTask cancelledTimer task finished at:Sun Aug 09 16:29:28 CST 2015
The output confirms that if a task is already executing, Timer will wait for it to finish and once finished, it will start again the next task from the queue.
Timer object can be created to run the associated tasks as a daemon thread. Timer cancel() method is used to terminate the timer and discard any scheduled tasks, however it doesn’t interfere with the currently executing task and let it finish. If the timer is run as daemon thread, whether we cancel it or not, it will terminate as soon as all the user threads are finished executing.
Timer class contains several schedule() methods to schedule a task to run once at given date or after some delay. There are several scheduleAtFixedRate() methods to run a task periodically with certain interval.
While scheduling tasks using Timer, you should make sure that time interval is more than normal thread execution, otherwise tasks queue size will keep growing and eventually task will be executing always.
- 【Java多线程】之十:Timer and TimerTask
- 【Java多线程】-Timer,TimerTask,ScheduledExecutorService
- Java并发之Timer,TimerTask
- Java多线程编程--(2)Timer & TimerTask
- Java 中 Timer 和 TimerTask实现多线程
- Java多线程编程--(2)Timer & TimerTask
- Java 多线程Timer 与 TimerTask 小案例
- Java多线程(五):Timer和TimerTask
- Timer and TimerTask
- Java定时调度之Timer/TimerTask
- java之Timer和TimerTask简单demo
- Java Timer和TimerTask
- Java中TimerTask、Timer
- JAVA Timer和TimerTask
- Java Timer和TimerTask
- java Timer和TimerTask
- Java Timer和Timertask
- Java多线程基础使用(Thread|Runable|TimerTask&Timer)
- UVa11038 How Many O's? (计数)
- 同步与异步的初步理解
- 让windows8 变成无线路由器的方法
- Leetcode51 N-Queens
- Sed简介
- 【Java多线程】之十:Timer and TimerTask
- Swift调用C代码的方法
- 华为上机 循环报数 java
- 零基础学python-1.4 hello world
- openssl 提取秘钥
- hdu5358 First One 尺取法 多校联合第六场
- conn.setAutoCommit()用法及作用
- 音量图标不显示了怎么恢复
- 架构师要以自己的编程能为依托