`
wisfly
  • 浏览: 60987 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

几种定时器介绍

 
阅读更多

1:java定时器的几种用法

  1. package com.lid;  
  2.   
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5. import java.util.Timer;  
  6. import java.util.TimerTask;  
  7.   
  8. public class Test {  
  9.     public static void main(String[] args) {  
  10.         //timer1();  
  11.         timer2();  
  12.         //timer3();  
  13.         //timer4();  
  14.     }  
  15.   
  16.     // 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time)  
  17.     public static void timer1() {  
  18.         Timer timer = new Timer();  
  19.         timer.schedule(new TimerTask() {  
  20.             public void run() {  
  21.                 System.out.println("-------设定要指定任务--------");  
  22.             }  
  23.         }, 2000);// 设定指定的时间time,此处为2000毫秒  
  24.     }  
  25.   
  26.     // 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行  
  27.     // schedule(TimerTask task, long delay, long period)  
  28.     public static void timer2() {  
  29.         Timer timer = new Timer();  
  30.         timer.schedule(new TimerTask() {  
  31.             public void run() {  
  32.                 System.out.println("-------设定要指定任务--------");  
  33.             }  
  34.         }, 10001000);  
  35.     }  
  36.   
  37.     // 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。  
  38.     // scheduleAtFixedRate(TimerTask task, long delay, long period)  
  39.     public static void timer3() {  
  40.         Timer timer = new Timer();  
  41.         timer.scheduleAtFixedRate(new TimerTask() {  
  42.             public void run() {  
  43.                 System.out.println("-------设定要指定任务--------");  
  44.             }  
  45.         }, 10002000);  
  46.     }  
  47.      
  48.     // 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行.  
  49.     // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period)  
  50.     public static void timer4() {  
  51.         Calendar calendar = Calendar.getInstance();  
  52.         calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时  
  53.         calendar.set(Calendar.MINUTE, 0);       // 控制分  
  54.         calendar.set(Calendar.SECOND, 0);       // 控制秒  
  55.   
  56.         Date time = calendar.getTime();         // 得出执行任务的时间,此处为今天的12:00:00  
  57.   
  58.         Timer timer = new Timer();  
  59.         timer.scheduleAtFixedRate(new TimerTask() {  
  60.             public void run() {  
  61.                 System.out.println("-------设定要指定任务--------");  
  62.             }  
  63.         }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行  
  64.     }  
  65. }  

2:spring中定时器的使用

 

在Spring中有两种方式可以实现定时器的功能,分别是Scheduled注释方式和XML配置方式,本博客将介绍如何在Spring中使用Scheduled注释方式的方式实现定时器的功能,代码及相应的解释如下:

代码1—Spring配置文件(applicationContext.xml文件):

 

 

 

<beans beans="" context="" http:="" schema="" spring-beans-2.5.xsd="" spring-context-2.5.xsd="" spring-task-3.2.xsd="" task="" www.springframework.org=""
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="http://www.springframework.org/schema/beans">
  <context:annotation-config>
    <context:component-scan base-package="com.ghj/">
      <task:executor id="executor" pool-size="5">
        <task:scheduler id="scheduler" pool-size="10">
           <task:annotation-driven executor="executor" scheduler="scheduler">
           </task:annotation-driven>
        </task:scheduler>
      </task:executor>
    </context:component-scan>
  </context:annotation-config>
</beans>

 

 

代码2——Spring定时器测试类(SpringTimerTest.java文件):

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.ghj.packageoftimer;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
/**
 * Spring定时器测试类
 *
 * @author 高焕杰
 */
@Component//将本类完成bean创建和自动依赖注入
public class SpringTimerTest{
     
    /**
     * Spring定时器测试方法
     *
     * @author 高焕杰
     */
    @Scheduled(cron = 0 0/1 * * * ?)//通过@Scheduled注释将该方法定义为Spring定时调用的方法,其中cron用于指明该方法被调用的时机
    public void test(){
        System.err.println(new SimpleDateFormat(yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒).format(new Date()));
    }
}

 

代码3——加载Spring配置文件并启动Spring定时器的类(StartSpringTimer.java文件):

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.ghj.packageoftest;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
/**
 * 加载Spring配置文件,启动Spring定时器
 *
 * @author 高焕杰
 */
public class StartSpringTimer {
 
    public static void main(String[] args){
        new ClassPathXmlApplicationContext(conf/spring/applicationContext.xml);
        System.out.println(加载Spring配置文件完毕,Spring定时器成功启动!!!);
    }
}

 

这种方式实现Spring定时器的优点:

采用这种方式实现定时器的功能是我最喜欢的,这种方式简单灵活——只要在已被加载到Spring容器中的类内的方法上添加Scheduled注释并指定该方法的时机即可。

答疑解惑:

如果你是有心人,你可能会有这样的疑惑加载中...:在采用这种方式实现的Spring定时器时被Scheduled注释的方法的访问权限有没有特别的要求,呵呵呵加载中...,这是没有什么特别的要求的,你完全可以把该方法定义为私有的,这样就不会把该方法暴漏出去了。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics