Java定时器的连两个小示例


java定时器在web中的应用还是颇为广泛的,比如我们玩的qq农场等等典型的例子,下面就是一个后台应用程序和web后台程序的两个例子
(1) 后台程
package com.test;
import java.io.IOException;
import java.util.Timer;
/**
* 定时器类Timer在java.util包中。使用时,先实例化,然后使用实例的schedule(TimerTask task, long delay)方法,
* 设定指定的任务task在指定的延迟delay后执行。定时器任务类TimerTask是抽象类,继承并重写其run()方法,可实现具体任务。
* schedule(TimerTask task, Date time)设定指定任务task在指定时间time执行。 
* cancel()方法结束这个定时器。 
* schedule(TimerTask task, long delay, long period)方法设定指定任务task在指定延迟delay后进行固定延迟peroid的执行。
* scheduleAtFixedRate(TimerTask task, long delay, long period)方法设定指定任务task在指定延迟delay后进行固定频率peroid的执行。 
* 要实现一个定时任务,运用java中的Timer和TimerTask类可以非常容易实现实时调用处理函数。这两个类使用起来非常方便,可以完成我们对定时器的绝大多数需要。
*/

public class TimerTest {
public static void main(String[] args) {
// 定义一个定时器
Timer timer =  new Timer();
//在1秒后执行任务,每次间隔2秒,如果传递一个Date的参数,就可以在某个固定的时间执行这个任务
timer.schedule( new MyTask(), 1000, 2000);

System. out.println( "___进入 while ___");
while ( true) {
try {
System. out.println( "c"+'c');
int ch=System.in.read();
System. out.println( "ch: "+ch);
if (ch-'c'==0) {
timer.cancel();  //退出任务
}
catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* TimerTask必须继承run()方法
*/

static class MyTask  extends java.util.TimerTask {
@Override
public void run() {
System. out.println( "********** 进入run()方法 ***********");
}
}
}
执行效果:
___进入 while ___
cc
********** 进入run()方法 ***********
********** 进入run()方法 ***********
********** 进入run()方法 ***********
********** 进入run()方法 ***********
..........
(2)Web程序调用,每次到12点时候就记录到一个文本信息中
定义类:SetTimerExecute
package com.test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* web定时器
* 继承TimerTask抽象类,必须重写contextDestroyed(销毁),contextInitialized(初始化),run(执行)方法
* 实现ServletContextListener接口
*/

public class SetTimerExecute  extends TimerTask  implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent sce) {
System. out.println(" ******** 服务器停止 ******");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System. out.println( "******** 服务器启动 ******");
// 新建一个时间控件
Timer timer =  new Timer();
timer.schedule( new SetTimerExecute(), 1000, 1000);
}

@Override
public void run() {
FileWriter fw =  null;  //创建文件流
BufferedWriter buf =  null;
try {
File file =  new File(" /test.txt");
if (!file.exists())
file.createNewFile();  //创建文件

fw =  new FileWriter(file, true);  // 是否在后面进行追加内容,如果为true的话进行追加内容
buf =  new BufferedWriter(fw);
Date day =  new Date();
System. out.println( "现在时刻:" + day.getHours());
// 每天上午10点写入到文件
if (day.getHours() == 12) {
System. out.println( "等于12点");
fw.write( "现在时刻,北京时间:" + day.getHours()+ "点 "+day.getMinutes()+ "分 "+day.getSeconds()+ " 秒");
buf.newLine();  //追加一行
}
catch (Exception e) {
e.printStackTrace();
finally {
try {
if (buf != null) {
buf.flush();  //把缓冲区的数据强行输出
}
if (fw != null) {
fw.close();  // 关闭流
}
catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
xmlns="http://java.sun.com/xml/ns/javaee
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name> 
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.test.SetTimerExecute</listener-class>
</listener>

</web-app>
运行时会在你运行环境的根目录下创建一个text的文本文件,没到12点就往这个文件中添加时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值