ITeye博客

几种定时器介绍

wisfly · 2016-02-26 16:49 · 学习笔记 · 行业应用 · Markdown

1:java定时器的几种用法

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

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注释的方法的访问权限有没有特别的要求,呵呵呵加载中...,这是没有什么特别的要求的,你完全可以把该方法定义为私有的,这样就不会把该方法暴漏出去了。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。