- package com.lid;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- public class Test {
- 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, 1000);
- }
- // 第三种方法:设定指定任务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);// 这里设定将延时每天固定执行
- }
- }
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>
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注释的方法的访问权限有没有特别的要求,呵呵呵,这是没有什么特别的要求的,你完全可以把该方法定义为私有的,这样就不会把该方法暴漏出去了。
相关推荐
javaweb的几种定时方式,有助于了解定时的工作原理。对于定时器不了解的朋友可以看看
主要介绍了c# 几种定时器(timer)的区别,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
java中实现定时器的三种方式,qutarz+spring, spring-task, java Timer
单片机C语言编程中,定时器的初值对于初学者真的是比较不好计算,因此我总结了以下几种方法。
MCS-51单片机含有2个定时器/计数器,具有4种工作方式。具有两种工作模式(计数器模式和定时器模式)MCS-51单片机含有1个全双工串行口,具有4种工作方式。TMOD-》定时器/计数器方式控制寄存器TCON-》定时器/计数器...
Spring中的Quartz配置-Spring-定时器-java定时器.doc
android几种定时器机制及区别,喜欢可以下载,需要技术支持可以加qq联系我。
SIMOTION 定时器的编程使用zip,SIMOTION 中提供了几种定时器,如TOF,TON及TP,本文分别介绍了这些定时器的编程及使用方法。
在android中,经常用到的定时器主要有以下几种实现: 一、采用Handler与线程的sleep(long )方法 二、采用Handler的postDelayed(Runnable, long) 方法 三、采用Handler与timer及TimerTask结合的方法。 三种方法都有...
android实现定时器有很多种,例子中选了Timer、CountDownTimer、handler+postDelay、handler+handler等几种实现了一下,欢迎下载!
本文对定时功能在PLC中的作用作了简单阐述,并主要对计时器定时、扫描周期累加编程定时、周期中断编程定时等三中实现方法和各自的特点进行了具体描述。
1.Ace种定时器实现有4种,这里不具体介绍实现细节,主要介绍实现数据结构,性能。 具体的4种定时器都是从ACE_Timer_Queue_T继承,每种定时器用不同的数据结构来实现具体Timer的算法。 1)ACE_Timer_Heap...
讲解定时器,比如定时器的几种方式。定时器如何操作。
AVR单片机中断、定时器,中有中断的几种常见模式的讲解。
该文档主要讲解了定时器的具体的配置规则,以及常用的几种时间配置
1、独有三种设置提醒时间的方式,令该程序更方便易用。 2、可以设置按时间、日期、星期、月份或年份进行定时提醒。 3、全面支持WINDOWS 9X/NT/2000/XP等操作系统定时注销、重启、关 闭计算机及断开网络连接,并...
Android中实现定时器的四种方式 第一种方式利用Timer和TimerTask 1、继承关系 java.util.Timer 基本方法 schedule 例如: timer.schedule(task, delay,period); //delay为long,period为long:从现在起过delay毫秒...
在Linux实现一个定时器,不像Win32下那样直观。在Win32调用SetTimer就行了,在Linux下则没有相应函数可以直接调用。定时器作为一个常用的功能,在Linux当然也有相应实现。本文介绍几种常用的方法。
本篇文章主要介绍了详解Android实现定时器的几种方法,主要包括了Handler, Timer, Thread, AlarmManager,有兴趣的可以了解一下