JAVA定时器使用,转载2~
JAVA定时器(java.util.Timer)
2009年07月30日 星期四 下午 02:17
【1】Java 定时器(java.util.Timer)有定时触发计划任务的功能,通过配置定时器的间隔时间,在某一间隔时间段之后会自动有规律的调用预先所安排的计划任务(java.util.TimerTask)。与每个 Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述不友好的任务最终完成时才能够被快速连续地执行。另外,由于我们希望当Web工程启动时,定时器能自动开始计时,在整个Web工程的生命期里,定时器能在每晚深夜触发一次报表计算引擎天等。因此定时器的存放位置也值得考查,不能简单的存在于单个Servlet或JavaBean中,必须能让定时器宿主的存活期为整个Web工程生命期,在工程启动时能自动加载运行。结合这两点,跟Servlet上下文有关的监听器就最合适不过了,通过在工程的配置文件中加以 合理配置,会在工程启动时自动运行,并在整个工程生命期中处于监听状态。
下面就Servlet监听器结合Java 定时器来讲述整个实现过程。要运用Servlet监听器需要实现javax.servlet.ServletContextListener接口,同时实现它的contextInitialized(ServletContextEvent event)和contextDestroyed(ServletContextEvent event)两个接口函数。考虑定时器有个建立和销毁的过程,看了前面两个接口函数,就不容置疑的把建立的过程置入 contextInitialized,把销毁的过程置入contextDestroyed了。
package com.korby.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import com.korby.pub.TimerManager;
/**
* 启动定时器的监听器
* @author korby
* @version 1.0
*/
public class TimerListener implements ServletContextListener {
private TimerManager tm = null;
public void contextInitialized(ServletContextEvent sce) {
int day,hour,min,sec;
day = 26; hour = 16; min = 42; sec = 0;
 
相关文档:
/*
* EncryptUtils.java
* Copyright (C) 2007-3-19 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* ......
/*
* CookieSupport.java
* Copyright (C) 2007-3-19 <JustinLei@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version ......
本文原址:http://blogger.org.cn/blog/more.asp?name=hongrui&id=46926
/**
* Java里数字转字符串前面自动补0的实现。
*
*/
public class TestStringFormat {
public static void ......
1.使用java.util.Properties类的load()方法
示例:
Java代码
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
InputStream ......