Java计时器Timer 使用
来源:互联网 发布:淘宝复古店铺推荐 编辑:程序博客网 时间:2024/04/28 01:15
所有类型的 Java 应用程序一般都需要计划重复执行的任务
Timer类是用来执行任务的类,它接受一个TimerTask做参数
java.util.Timer 和 java.util.TimerTask ,它们使程序员可以很容易地计划简单的任务
Timer
Timer最常用的是schedule执行任务的模式,,它可以以两种方式执行任务:
1:在某个时间(Data),2:在某个固定的时间之后(int delay).这两种方式都可以指定任务执行的频率.
看个简单的例子:
- import java.io.IOException;
- import java.util.Timer;
- public class TimerTest {
- public static void main(String[] args){
- Timer timer = new Timer();
- timer.schedule(new Job(), 5000, 10000);//在5秒后执行此任务,每次间隔60秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
- /*
- //这个是用来停止此任务的,否则就一直循环执行此任务了
- while(1==1){
- try {
- if(2==2){
- timer.cancel();//使用这个方法退出任务
- }
- } catch (IOException e)
- e.printStackTrace();
- } */
- }
- static class Job extends java.util.TimerTask{
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println("so...easy!");
- }
- }
- }
- import java.io.IOException;
- import java.util.Timer;
- public class TimerTest {
- public static void main(String[] args){
- Timer timer = new Timer();
- timer.schedule(new Job(), 5000, 10000);//在5秒后执行此任务,每次间隔60秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
- /*
- //这个是用来停止此任务的,否则就一直循环执行此任务了
- while(1==1){
- try {
- if(2==2){
- timer.cancel();//使用这个方法退出任务
- }
- } catch (IOException e)
- e.printStackTrace();
- } */
- }
- static class Job extends java.util.TimerTask{
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println("so...easy!");
- }
- }
- }
TimerTask 刚才在代码里提到了 TimerTask 类。TimerTask类里有一个抽象方法run(),我们把任务写到run()方法里,或由run()执行其他方法.
完整的代码:
- public class TimerTest{
- public static void main(String[] args){
- int delay=5000;//延迟5秒
- Timer timer=new Timer();//生成一个Timer对象
- NewTask myTask=new NewTask();//初始化我们的任务
- timer.schedule(yourTask,delay);//还有其他重载方法...
- }
- }
- class NewTask extends TimerTask{//继承TimerTask类
- public void run(){
- System.out.println("printing!");
- }
- }
- public class TimerTest{
- public static void main(String[] args){
- int delay=5000;//延迟5秒
- Timer timer=new Timer();//生成一个Timer对象
- NewTask myTask=new NewTask();//初始化我们的任务
- timer.schedule(yourTask,delay);//还有其他重载方法...
- }
- }
- class NewTask extends TimerTask{//继承TimerTask类
- public void run(){
- System.out.println("printing!");
- }
- }
这样这个程序就会在五秒钟后输出 printing!
Timer的功能是相当强大的,若要详细了解,可以查看帮助文档.完整案例!
现在大家看一下我写的关于实现LED滚屏显示动态信息!
web 监听:
- package com.ving.xzfw.led;
- import java.util.Timer;//定时器类
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.ServletContext;
- import org.apache.log4j.Logger;
- import com.ving.xzfw.util.GetInformation;
- //import com.ving.xzfw.lwsp.GetConfigInfor;
- public class LEDListener implements ServletContextListener {
- private Timer timer = null;
- private Logger log = Logger.getLogger(getClass());
- // GetconfigInfor config = new GetconfigInfor();
- GetInformation getInfo = new GetInformation("bpelConfig.properties");
- Integer runTime = Integer.parseInt(getInfo.getProperty("led.TimingRunTime"));
- String fileRealPath = "";
- String templePath = "";
- public void contextInitialized(ServletContextEvent event) {
- // 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能
- ServletContext context = event.getServletContext();
- fileRealPath = context.getRealPath("")
- + System.getProperty("file.separator") + "LEDFile"
- + System.getProperty("file.separator");
- templePath = context.getRealPath("")
- + System.getProperty("file.separator") + "led"
- + System.getProperty("file.separator");
- timer = new Timer();
- log.info("定时器已启动");// 添加日志,可在tomcat日志中查看到
- timer.schedule(new LEDTimerTack(fileRealPath, templePath), 20000, runTime);
- log.info("已经添加任务");
- }
- public void contextDestroyed(ServletContextEvent event) {// 在这里关闭监听器,所以在这里销毁定时器。
- timer.cancel();
- log.info("定时器销毁");
- }
- }
- package com.ving.xzfw.led;
- import java.util.Timer;//定时器类
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.ServletContext;
- import org.apache.log4j.Logger;
- import com.ving.xzfw.util.GetInformation;
- //import com.ving.xzfw.lwsp.GetConfigInfor;
- public class LEDListener implements ServletContextListener {
- private Timer timer = null;
- private Logger log = Logger.getLogger(getClass());
- //GetconfigInfor config = new GetconfigInfor();
- GetInformation getInfo = new GetInformation("bpelConfig.properties");
- Integer runTime = Integer.parseInt(getInfo.getProperty("led.TimingRunTime"));
- String fileRealPath = "";
- String templePath = "";
- public void contextInitialized(ServletContextEvent event) {
- // 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能
- ServletContext context = event.getServletContext();
- fileRealPath = context.getRealPath("")
- + System.getProperty("file.separator") + "LEDFile"
- + System.getProperty("file.separator");
- templePath = context.getRealPath("")
- + System.getProperty("file.separator") + "led"
- + System.getProperty("file.separator");
- timer = new Timer();
- log.info("定时器已启动");// 添加日志,可在tomcat日志中查看到
- timer.schedule(new LEDTimerTack(fileRealPath, templePath), 20000, runTime);
- log.info("已经添加任务");
- }
- public void contextDestroyed(ServletContextEvent event) {// 在这里关闭监听器,所以在这里销毁定时器。
- timer.cancel();
- log.info("定时器销毁");
- }
- }
- package com.ving.xzfw.led;
- import java.util.TimerTask;
- public class LEDTimerTack extends TimerTask {
- String fileRealPath = "";
- String templePath = "";
- String type="";
- int floor;
- public LEDTimerTack(String fileRealPath,String templePath,String type,Integer floor) {
- this.fileRealPath=fileRealPath;
- this.templePath=templePath;
- this.type =type;
- this.floor=floor;
- }
- public LEDTimerTack(String fileRealPath,String templePath) {
- this.fileRealPath=fileRealPath;
- this.templePath=templePath;
- }
- public void run( ) {
- for(int i=1;i<=3;i++)
- //呵呵,这里就是led动态生成信息的代码!
- XlsUtil.createHtmlFile(templePath, fileRealPath,i);
- }
- }
- Java计时器Timer 使用
- [转]Java计时器Timer 使用
- Java计时器Timer的使用!...
- java Timer类的使用(计时器)
- Java计时器Timer 使用1 (转)
- 【Android】计时器Timer使用
- java 计时器timer
- java timer计时器
- Java 计时器Timer
- Java计时器Timer 使用 完整案例(转)
- timer的使用--倒计时--计时器
- java 计时器线程 Timer类
- Timer计时器
- Timer计时器
- 计时器Timer
- Timer计时器
- Timer计时器
- Timer 计时器
- 利用ClassLoader实现动态热布署
- URI、URL、URN
- JQuery 实现清空页面所有的文本框和文本域
- js正则表达式有关的各种方法
- PKU ACM poj 1175 源代码
- Java计时器Timer 使用
- TQ2440裸奔程序1-LED流水灯
- AspNet pager 分页的基本用法
- Oracle中表级列的默认值控制
- 准备使用Windows Live Writer
- Andriod SDK Window安装步骤 (myeclipse)
- 带有密匙的安装程序制作
- rsync
- RHEL6.0 QEMU/KVM 建立新的虚拟机之配置Birdged Networking