java后台当中的定时器的4种使用方式

下面为大家介绍java后台的第一种定时器处理数据的4种方法:
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimeTest {
    public static void main(String[] args) {
        timer1();
        //timer2();
        //timer3();
        //timer4();
    }

    // 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)
    public static void timer1() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("-------在这里写要处理的事情就可以了--------");
            }
        }, 2000);// 设定指定的时间time,此处为2000毫秒
    }

    // 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行
    // schedule(TimerTask task, long delay, long period)
    public static void timer2() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("-------设定要指定任务--------");
            }
        }, 1000, 5000);
    }

    // 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。
    // scheduleAtFixedRate(TimerTask task, long delay, long period)
    public static void timer3() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("-------在这里写要处理的事情就可以了--------");
            }
        }, 1000, 2000);
    }
    // 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.
    // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)
    public static void timer4() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时
        calendar.set(Calendar.MINUTE, 0);       // 控制分
        calendar.set(Calendar.SECOND, 0);       // 控制秒

        Date time = calendar.getTime();         // 得出执行任务的时间,此处为今天的12:00:00

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("-------在这里写要处理的事情就可以了--------");
            }
        }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
    }
}

*既然监听是在服务启动的时候就有了,自然离不开配置文件了。那web.xml配置文件是如何配置的呢?

<listener>
         <!—此处为实现监听接口的完整的包名类名-->
       <listener-class>
                com.boco.TimeTest
       </listener-class>
</listener>
下面为大家介绍java后台的第二种定时器处理数据:

要运用Servlet侦听器需要实现javax.servlet.ServletContextListener接口,同时实现它的contextInitialized(ServletContextEvent event)和contextDestroyed(ServletContextEvent event)两个接口函数。

public class MyListener implements ServletContextListener { 
 
    private java.util.Timer timer = null ; 
    public void contextDestroyed(ServletContextEvent event) { 
        // TODO Auto-generated method stub 
 
    } 
 
    public void contextInitialized(ServletContextEvent event) { 
   Calendar calendar = Calendar.getInstance();
         calendar.set(Calendar.HOUR_OF_DAY, 10); // 控制时
         calendar.set(Calendar.MINUTE, 57);       // 控制分
         calendar.set(Calendar.SECOND, 0);       // 控制秒

         Date time = calendar.getTime();         // 得出执行任务的时间,此处为今天的12:00:00
       
         Timer timer = new Timer();
         timer.scheduleAtFixedRate(new TimerTask() {
             public void run() {
                 System.out.println("-------在这里写要处理的事情就可以了--------");
             }
         }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
    } 
 
}  

contextInitialized函数里的内容将被自动执行 最后在web.xml里面添加一个监听节点就行了

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
  <display-name>myweb2</display-name> 
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
  <listener> 
    <listener-class>org.cai.MyListener</listener-class> 
  </listener> 
</web-app>  
正在加载评论...