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>
相关文档:
有1亿个浮点数,请找出其中对大的10000个。提示:假设每个浮点数占4个字节,1亿个浮点数就要站到相当大的空间,因此不能一次将全部读入内存进行排序。
/**
*
*/
package com.code;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOu ......
一、写pdf
需要包:iText-2.1.0.jar
中文处理:iTextAsian.jar
1、HelloWorld例子
package com.my.file.pdf;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class HelloWorld ......
注意事项慢慢积累
final 类不可被继承,也不能有子类。
final class Book{}
//class ComBook extends Book{} 这将编译失败,因为Book类不可被继承。
final方法不可被改写
class Book{
final void show(){System.out.println("Book ......
在网上找的一些资料~~想存起来~~方便以后查看
学习Java Swing图形化编程,我们首先要了解三个最基本的概念:顶层容器,控件,布局。
下面就来介绍一下这三个基本概念
1.顶层容器
什么是顶层容器?当我们使用Java进行图形编程的时候,图在哪里绘制呢?我们需要一个能够提供图形绘制的容器, ......
本文介绍如何在Linux下配置Java环境变量。配置共分十个步骤,从下载JDK开始,到Linux上JDK的安装,Eclipse的相应配置,最终在桌面上创建一个启动器,路径设置到Eclipse,就此完成Java环境变量的配置。
1.去http://java.sun.com/j2se/1.4.2/download.html
下载一个Linux Platform的JDK,
建议下载RPM自解压格式的(RP ......