易截截图软件、单文件、免安装、纯绿色、仅160KB

JAVA定时执行任务的三种方法

JAVA定时执行任务的三种方法 
1) java.util.Timer
这个方法应该是最常用的,不过这个方法需要手工启动你的任务:
Timer timer=new Timer();
timer.schedule(new ListByDayTimerTask(),10000,86400000);
这里的ListByDayTimerTask类必须extends TimerTask里面的run()方法。
2) ServletContextListener
这个方法在web容器环境比较方便,这样,在web server启动后就可以
自动运行该任务,不需要手工操作。
将ListByDayListener implements ServletContextListener接口,在
contextInitialized方法中加入启动Timer的代码,在contextDestroyed
方法中加入cancel该Timer的代码;然后在web.xml中,加入listener:
< listener>
< listener-class>com.qq.customer.ListByDayListener< /listener-class>
< /listener>
3)org.springframework.scheduling.timer.ScheduledTimerTask
如果你用spring,那么你不需要写Timer类了,在schedulingContext-timer
.xml中加入下面的内容就可以了:
< ?xml version="1.0" encoding="UTF-8"?>
< !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
< beans>
< bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean">
< property name="scheduledTimerTasks">
< list>
< ref local="MyTimeTask1"/>
< /list>
< /property>
< /bean>
< bean id="MyTimeTask" class="com.qq.timer.ListByDayTimerTask"/>
< bean id="MyTimeTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask">
< property name="timerTask">
< ref bean="MyTimeTask"/>
< /property>
< property name="delay">
< value>10000< /value>
< /property>
< property name="period">
< value>86400000< /value>
< /property>
< /bean>
< /beans> 


相关文档:

java中内部类的使用

内部类的使用的优缺点:
优点:减少了在类文件编译后的产生的字节码文件的大小
缺点:使程序结构不清晰
使用内部类的注意事项:
内部类的使用一般都与所在的外部类有一定的关联,它是在一个类的内部嵌套定义的类,它可以是其它类的成员,也可以在一个语句块的内部定义,还可以在表达式内部匿名定义(匿名内部类)。
内部 ......

记一:Java之可视化界面swing设计

     在网上找的一些资料~~想存起来~~方便以后查看
学习Java Swing图形化编程,我们首先要了解三个最基本的概念:顶层容器,控件,布局。
下面就来介绍一下这三个基本概念
1.顶层容器
什么是顶层容器?当我们使用Java进行图形编程的时候,图在哪里绘制呢?我们需要一个能够提供图形绘制的容器, ......

Java压缩与解压rar文件

package com.sunz.fileUpload;
public class RarToFile {
   
    //cmd 压缩与解压缩命令
    private static String rarCmd = "C:\\Program Files\\WinRAR\\Rar.exe a ";
    private static String unrarCmd = "C:\\Program Files\\W ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号