所有类型的 Java 应用程序一般都需要计划重复执行的任务
Timer类是用来执行任务的类,它接受一个TimerTask做参数
java.util.Timer 和 java.util.TimerTask ,它们使程序员可以很容易地计划简单的任务
Timer
Timer最常用的是schedule执行任务的模式,,它可以以两种方式执行任务:
1:在某个时间(Data),2:在某个固定的时间之后(int delay).这两种方式都可以指定任务执行的频率.
看个简单的例子:
- importjava.io.IOException;
- importjava.util.Timer;
- publicclassTimerTest{
- publicstaticvoidmain(String[]args){
- Timertimer=newTimer();
- timer.schedule(newJob(),5000,10000);//在5秒后执行此任务,每次间隔60秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
- /*
- //这个是用来停止此任务的,否则就一直循环执行此任务了
- while(1==1){
- try{
- if(2==2){
- timer.cancel();//使用这个方法退出任务
- }
- }catch(IOExceptione)
- e.printStackTrace();
- }*/
- }
- staticclassJobextendsjava.util.TimerTask{
- @Override
- publicvoidrun(){
- //TODOAuto-generatedmethodstub
- System.out.println("so...easy!");
- }
- }
- }
- importjava.io.IOException;
- importjava.util.Timer;
- publicclassTimerTest{
- publicstaticvoidmain(String[]args){
- Timertimer=newTimer();
- timer.schedule(newJob(),5000,10000);//在5秒后执行此任务,每次间隔60秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
- /*
- //这个是用来停止此任务的,否则就一直循环执行此任务了
- while(1==1){
- try{
- if(2==2){
- timer.cancel();//使用这个方法退出任务
- }
- }catch(IOExceptione)
- e.printStackTrace();
- }*/
- }
- staticclassJobextendsjava.util.TimerTask{
- @Override
- publicvoidrun(){
- //TODOAuto-generatedmethodstub
- 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!"); } } } 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()执行其他方法.
完整的代码:
- publicclassTimerTest{
- publicstaticvoidmain(String[]args){
- intdelay=5000;//延迟5秒
- Timertimer=newTimer();//生成一个Timer对象
- NewTaskmyTask=newNewTask();//初始化我们的任务
- timer.schedule(yourTask,delay);//还有其他重载方法...
- }
- }
- classNewTaskextendsTimerTask{//继承TimerTask类
- publicvoidrun(){
- System.out.println("printing!");
- }
- }
- publicclassTimerTest{
- publicstaticvoidmain(String[]args){
- intdelay=5000;//延迟5秒
- Timertimer=newTimer();//生成一个Timer对象
- NewTaskmyTask=newNewTask();//初始化我们的任务
- timer.schedule(yourTask,delay);//还有其他重载方法...
- }
- }
- classNewTaskextendsTimerTask{//继承TimerTask类
- publicvoidrun(){
- 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!"); } } 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 监听:
- packagecom.ving.xzfw.led;
- importjava.util.Timer;//定时器类
- importjavax.servlet.ServletContextEvent;
- importjavax.servlet.ServletContextListener;
- importjavax.servlet.ServletContext;
- importorg.apache.log4j.Logger;
- importcom.ving.xzfw.util.GetInformation;
- //importcom.ving.xzfw.lwsp.GetConfigInfor;
- publicclassLEDListenerimplementsServletContextListener{
- privateTimertimer=null;
- privateLoggerlog=Logger.getLogger(getClass());
- //GetconfigInforconfig=newGetconfigInfor();
- GetInformationgetInfo=newGetInformation("bpelConfig.properties");
- IntegerrunTime=Integer.parseInt(getInfo.getProperty("led.TimingRunTime"));
- StringfileRealPath="";
- StringtemplePath="";
- publicvoidcontextInitialized(ServletContextEventevent){
- //在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能
- ServletContextcontext=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=newTimer();
- log.info("定时器已启动");//添加日志,可在tomcat日志中查看到
- timer.schedule(newLEDTimerTack(fileRealPath,templePath),20000,runTime);
- log.info("已经添加任务");
- }
- publicvoidcontextDestroyed(ServletContextEventevent){//在这里关闭监听器,所以在这里销毁定时器。
- timer.cancel();
- log.info("定时器销毁");
- }
- }
- packagecom.ving.xzfw.led;
- importjava.util.Timer;//定时器类
- importjavax.servlet.ServletContextEvent;
- importjavax.servlet.ServletContextListener;
- importjavax.servlet.ServletContext;
- importorg.apache.log4j.Logger;
- importcom.ving.xzfw.util.GetInformation;
- //importcom.ving.xzfw.lwsp.GetConfigInfor;
- publicclassLEDListenerimplementsServletContextListener{
- privateTimertimer=null;
- privateLoggerlog=Logger.getLogger(getClass());
- //GetconfigInforconfig=newGetconfigInfor();
- GetInformationgetInfo=newGetInformation("bpelConfig.properties");
- IntegerrunTime=Integer.parseInt(getInfo.getProperty("led.TimingRunTime"));
- StringfileRealPath="";
- StringtemplePath="";
- publicvoidcontextInitialized(ServletContextEventevent){
- //在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能
- ServletContextcontext=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=newTimer();
- log.info("定时器已启动");//添加日志,可在tomcat日志中查看到
- timer.schedule(newLEDTimerTack(fileRealPath,templePath),20000,runTime);
- log.info("已经添加任务");
- }
- publicvoidcontextDestroyed(ServletContextEventevent){//在这里关闭监听器,所以在这里销毁定时器。
- 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.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("定时器销毁"); } }
- packagecom.ving.xzfw.led;
- importjava.util.TimerTask;
- publicclassLEDTimerTackextendsTimerTask{
- StringfileRealPath="";
- StringtemplePath="";
- Stringtype="";
- intfloor;
- publicLEDTimerTack(StringfileRealPath,StringtemplePath,Stringtype,Integerfloor){
- this.fileRealPath=fileRealPath;
- this.templePath=templePath;
- this.type=type;
- this.floor=floor;
- }
- publicLEDTimerTack(StringfileRealPath,StringtemplePath){
- this.fileRealPath=fileRealPath;
- this.templePath=templePath;
- }
- publicvoidrun(){
- for(inti=1;i<=3;i++)
- //呵呵,这里就是led动态生成信息的代码!
- XlsUtil.createHtmlFile(templePath,fileRealPath,i);
- }
- }
相关推荐
Java计时器Timer 使用
Java计时器Timer和TimerTask用法总结
使用java编写的计时器,调用Timer类,实现开始,暂停,继续,停止,代码量小,易懂
有个简单的java 的例子描述对java的Timer的使用的介绍,与实现的例子,方便大家学习
计时器程序的代码
将Timer.jar放入 开始->所有程序->启动 这个文件夹中 当你下次开机时它将自动启动从而开始计时 在C:\TimeLog文件夹下就可以看到记录日志
java倒计时器代码,主要是怎样利用java来编写倒计时器的代码
下载解压后有两个java类,自己创建java项目,把两个java类复制进去,修改下包路径即可运行!
计时器计时功能的控制用到了Date类和Timer类,为了让所做的图形界面程序通过事件响应机制实现用户与程序的交互,通过在按钮单击事件里添加一个监控对象,然后使用接口对象的方法来完成相应事件的处理。本程序中还...
计时器NetBeans IDE中的计时器。 使用Java
order_timer这是一个下单后等待结算倒计时的计时器程序,前端使用js进行倒计时显示,后端开辟新的线程进行倒计时及定时相关操作程序原理java.util包中提供了两个非常有用类Timer 、TimerTask。Timer类用来安排任务...
拳击间隔计时器:Android 应用程序适用于 HIIT、MMA、拳击、Tabata 和任何形式的间歇锻炼的计时器。描述拳击间隔计时器是一个计时器,可帮助您专注于锻炼或训练,而无需关心时间。 它是为拳击而开发的,但它可用于 ...
GIF 计时器 有时,拥有一个不属于其他程序或基于在线的倒数计时器很有用。 到处都支持动画 GIF(甚至在 PowerPoint 和 Keynote 中),并且非常适合用作计时器。 这两个脚本使用 ImageMagick 生成持续任意持续时间的...
应用启动页自定义跳转计时器View Demo: CircleTextProgressbar.java: package com.demo.startpageskiptimerdemo.widget; import android.content.Context; import android.content.res.ColorStateList; import ...
现在它是一个库Usage timer = Rx2Timer.builder().initialDelay(0)//默认为0这是一个简单的rxjava2计时器。 我将此课程复制到我制作的所有小应用程序中。 我已经厌倦了。 现在是一个库用法计时器= Rx2Timer....
安卓定时器 Android 计时器示例
基本涵盖java多线程的概念,适合初学者
随机计时器 随机计时器是一款专为聚会游戏和其他情况而设计的应用程序,在这种情况下,您需要倒计时,该倒计时将在随机的时间后终止,并且参与者不知道还剩下多少时间。 当前功能包括: 可配置的最大值和最小值,将...
javax.management.timer 提供对 Timer MBean(计时器 MBean)的定义。 javax.naming 为访问命名服务提供类和接口。 javax.naming.directory 扩展 javax.naming 包以提供访问目录服务的功能。 javax.naming.event ...