Java计时器Timer 使用

来源:互联网 发布:淘宝复古店铺推荐 编辑:程序博客网 时间:2024/04/28 01:15

所有类型的 Java 应用程序一般都需要计划重复执行的任务

Timer类是用来执行任务的类,它接受一个TimerTask做参数

java.util.Timer 和 java.util.TimerTask ,它们使程序员可以很容易地计划简单的任务

Timer

Timer最常用的是schedule执行任务的模式,,它可以以两种方式执行任务:

1:在某个时间(Data),2:在某个固定的时间之后(int delay).这两种方式都可以指定任务执行的频率.

看个简单的例子:

Java代码 复制代码
  1. import java.io.IOException;   
  2.   
  3.     import java.util.Timer;   
  4.   
  5.     public class TimerTest {   
  6.   
  7.     public static void main(String[] args){   
  8.   
  9.     Timer timer = new Timer();   
  10.   
  11.     timer.schedule(new Job(), 500010000);//在5秒后执行此任务,每次间隔60秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.   
  12.   
  13.     /*  
  14.  
  15.     //这个是用来停止此任务的,否则就一直循环执行此任务了  
  16.  
  17.     while(1==1){  
  18.  
  19.     try {  
  20.  
  21.     if(2==2){  
  22.  
  23.     timer.cancel();//使用这个方法退出任务  
  24.  
  25.     }  
  26.  
  27.     } catch (IOException e)  
  28.  
  29.     e.printStackTrace();  
  30.  
  31.     } */  
  32.   
  33.     }   
  34.   
  35.     static class Job extends java.util.TimerTask{   
  36.   
  37.     @Override  
  38.   
  39.     public void run() {   
  40.   
  41.     // TODO Auto-generated method stub   
  42.   
  43.     System.out.println("so...easy!");   
  44.   
  45.     }   
  46.   
  47.     }   
  48.   
  49.     }   
  50.   
  51.     import java.io.IOException;   
  52.   
  53.     import java.util.Timer;   
  54.   
  55.     public class TimerTest {   
  56.   
  57.     public static void main(String[] args){   
  58.   
  59.     Timer timer = new Timer();   
  60. timer.schedule(new Job(), 500010000);//在5秒后执行此任务,每次间隔60秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.   
  61.   
  62.     /*  
  63.  
  64.     //这个是用来停止此任务的,否则就一直循环执行此任务了  
  65.  
  66.     while(1==1){  
  67.  
  68.     try {  
  69.  
  70.     if(2==2){  
  71.  
  72.     timer.cancel();//使用这个方法退出任务  
  73.  
  74.     }  
  75.  
  76.     } catch (IOException e)  
  77.  
  78.     e.printStackTrace();  
  79.  
  80.     } */  
  81.   
  82.     }   
  83.   
  84.     static class Job extends java.util.TimerTask{   
  85.   
  86.     @Override  
  87.   
  88.     public void run() {   
  89.   
  90.     // TODO Auto-generated method stub   
  91.   
  92.     System.out.println("so...easy!");   
  93.   
  94.     }   
  95.   
  96.     }   
  97.   
  98.     }  

 

TimerTask 刚才在代码里提到了 TimerTask 类。TimerTask类里有一个抽象方法run(),我们把任务写到run()方法里,或由run()执行其他方法.

    完整的代码:

Java代码 复制代码
  1. public class TimerTest{   
  2.   
  3.     public static void main(String[] args){   
  4.   
  5.     int delay=5000;//延迟5秒   
  6.   
  7.     Timer timer=new Timer();//生成一个Timer对象   
  8.   
  9.     NewTask myTask=new NewTask();//初始化我们的任务   
  10.   
  11.     timer.schedule(yourTask,delay);//还有其他重载方法...   
  12.   
  13.     }   
  14.   
  15.     }   
  16.   
  17.     class NewTask extends TimerTask{//继承TimerTask类   
  18.   
  19.     public void run(){   
  20.   
  21.     System.out.println("printing!");   
  22.   
  23.     }   
  24.   
  25.     }   
  26.   
  27.     public class TimerTest{   
  28.   
  29.     public static void main(String[] args){   
  30.   
  31.     int delay=5000;//延迟5秒   
  32.   
  33.     Timer timer=new Timer();//生成一个Timer对象   
  34.   
  35.     NewTask myTask=new NewTask();//初始化我们的任务   
  36.   
  37.     timer.schedule(yourTask,delay);//还有其他重载方法...   
  38.   
  39.     }   
  40.   
  41.     }   
  42.   
  43.     class NewTask extends TimerTask{//继承TimerTask类   
  44.   
  45.     public void run(){   
  46.   
  47.     System.out.println("printing!");   
  48.   
  49.     }   
  50.   
  51.     }  

 

这样这个程序就会在五秒钟后输出 printing!

    Timer的功能是相当强大的,若要详细了解,可以查看帮助文档.完整案例!

    现在大家看一下我写的关于实现LED滚屏显示动态信息!

    web 监听:

Java代码 复制代码
  1. package com.ving.xzfw.led;   
  2.   
  3.     import java.util.Timer;//定时器类   
  4.   
  5.     import javax.servlet.ServletContextEvent;   
  6.   
  7.     import javax.servlet.ServletContextListener;   
  8.   
  9.     import javax.servlet.ServletContext;   
  10.   
  11.     import org.apache.log4j.Logger;   
  12.   
  13.     import com.ving.xzfw.util.GetInformation;   
  14.   
  15.     //import com.ving.xzfw.lwsp.GetConfigInfor;   
  16.   
  17.     public class LEDListener implements ServletContextListener {   
  18.   
  19.     private Timer timer = null;   
  20.   
  21.     private Logger log = Logger.getLogger(getClass());   
  22.   
  23.     //  GetconfigInfor config = new GetconfigInfor();   
  24.   
  25.     GetInformation getInfo = new GetInformation("bpelConfig.properties");   
  26.   
  27.     Integer runTime = Integer.parseInt(getInfo.getProperty("led.TimingRunTime"));   
  28.   
  29.     String fileRealPath = "";   
  30.   
  31.     String templePath = "";   
  32.   
  33.     public void contextInitialized(ServletContextEvent event) {   
  34.   
  35.     // 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能   
  36.   
  37.     ServletContext context = event.getServletContext();   
  38.   
  39.     fileRealPath = context.getRealPath("")   
  40.   
  41.     + System.getProperty("file.separator") + "LEDFile"  
  42.   
  43.     + System.getProperty("file.separator");   
  44.   
  45.     templePath = context.getRealPath("")   
  46.   
  47.     + System.getProperty("file.separator") + "led"  
  48.   
  49.     + System.getProperty("file.separator");   
  50.   
  51.     timer = new Timer();   
  52.   
  53.     log.info("定时器已启动");// 添加日志,可在tomcat日志中查看到   
  54.   
  55.     timer.schedule(new LEDTimerTack(fileRealPath, templePath), 20000, runTime);   
  56.   
  57.     log.info("已经添加任务");   
  58.   
  59.     }   
  60.   
  61.     public void contextDestroyed(ServletContextEvent event) {// 在这里关闭监听器,所以在这里销毁定时器。   
  62.   
  63.     timer.cancel();   
  64.   
  65.     log.info("定时器销毁");   
  66.   
  67.     }   
  68.   
  69.     }   
  70.   
  71.     package com.ving.xzfw.led;   
  72.   
  73.     import java.util.Timer;//定时器类   
  74.   
  75.     import javax.servlet.ServletContextEvent;   
  76.   
  77.     import javax.servlet.ServletContextListener;   
  78.   
  79.     import javax.servlet.ServletContext;   
  80.   
  81.     import org.apache.log4j.Logger;   
  82.   
  83.     import com.ving.xzfw.util.GetInformation;   
  84.   
  85.     //import com.ving.xzfw.lwsp.GetConfigInfor;   
  86.   
  87.     public class LEDListener implements ServletContextListener {   
  88.   
  89.     private Timer timer = null;   
  90.   
  91.     private Logger log = Logger.getLogger(getClass());   
  92.   
  93.     //GetconfigInfor config = new GetconfigInfor();   
  94.   
  95.     GetInformation getInfo = new GetInformation("bpelConfig.properties");   
  96.   
  97.     Integer runTime = Integer.parseInt(getInfo.getProperty("led.TimingRunTime"));   
  98.   
  99.     String fileRealPath = "";   
  100.   
  101.     String templePath = "";   
  102. public void contextInitialized(ServletContextEvent event) {   
  103.   
  104.     // 在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能   
  105.   
  106.     ServletContext context = event.getServletContext();   
  107.   
  108.     fileRealPath = context.getRealPath("")   
  109.   
  110.     + System.getProperty("file.separator") + "LEDFile"  
  111.   
  112.     + System.getProperty("file.separator");   
  113.   
  114.     templePath = context.getRealPath("")   
  115.   
  116.     + System.getProperty("file.separator") + "led"  
  117.   
  118.     + System.getProperty("file.separator");   
  119.   
  120.     timer = new Timer();   
  121.   
  122.     log.info("定时器已启动");// 添加日志,可在tomcat日志中查看到   
  123.   
  124.     timer.schedule(new LEDTimerTack(fileRealPath, templePath), 20000, runTime);   
  125.   
  126.     log.info("已经添加任务");   
  127.   
  128.     }   
  129.   
  130.     public void contextDestroyed(ServletContextEvent event) {// 在这里关闭监听器,所以在这里销毁定时器。   
  131.   
  132.     timer.cancel();   
  133.   
  134.     log.info("定时器销毁");   
  135.   
  136.     }   
  137.   
  138.     }  

 

Java代码 复制代码
  1. package com.ving.xzfw.led;   
  2.   
  3.     import java.util.TimerTask;   
  4.   
  5.     public class LEDTimerTack extends TimerTask {   
  6.   
  7.     String fileRealPath = "";   
  8.   
  9.     String templePath = "";   
  10.   
  11.     String type="";   
  12.   
  13.     int floor;   
  14.   
  15.     public LEDTimerTack(String fileRealPath,String templePath,String type,Integer floor) {   
  16.   
  17.     this.fileRealPath=fileRealPath;   
  18.   
  19.     this.templePath=templePath;   
  20.   
  21.     this.type =type;   
  22.   
  23.     this.floor=floor;   
  24.   
  25.     }   
  26.   
  27.     public LEDTimerTack(String fileRealPath,String templePath) {   
  28.   
  29.     this.fileRealPath=fileRealPath;   
  30.   
  31.     this.templePath=templePath;   
  32.   
  33.     }   
  34.   
  35.     public void run( ) {   
  36.   
  37.     for(int i=1;i<=3;i++)   
  38.   
  39.     //呵呵,这里就是led动态生成信息的代码!   
  40.   
  41.     XlsUtil.createHtmlFile(templePath, fileRealPath,i);   
  42.   
  43.     }   
  44.   
  45.     }  
原创粉丝点击