您好,登录后才能下订单哦!
小编给大家分享一下java中Timer定时器怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
定时计划任务功能在Java中主要使用的就是Timer对象,它在内部使用多线程的方式进行处理,所以它和多线程技术还是有非常大的关联的。在JDK中Timer类主要负责计划任务的功能,也就是在指定的时间开始执行某一个任务,但封装任务的类却是TimerTask类。
我们使用timer的时候,一般有4种情况:
指定时间执行
指定时间执行后间隔指定时间重复执行
启动任务之后多久执行
启动任务后多久执行,执行之后指定间隔多久重复执行
首先要通过继承 TimerTask 类 并实现 run() 方法来自定义要执行的任务(当然也可以写成匿名内部类),
需要创建一个定时器(Timer类对象),并通过Timer.schedule(TimerTask task,Date time) 方法执行时间运行任务
具体代码如下:
package timerdemo; 
import java.util.Timer;
import java.util.TimerTask; 
public class TimerDemo {
	public static void main(String[] args) {
		timerTest();
	}
	
	public static void timerTest(){
		//创建一个定时器
		Timer timer = new Timer();
		//schedule方法是执行时间定时任务的方法
		timer.schedule(new TimerTask() {
			
			//run方法就是具体需要定时执行的任务
			@Override
			public void run() {
				System.out.println("timer测试!!!");
			}
		}, 1000, 10000);
	}
}这里的 schedule方法有4个,分别对应上面说的四种情况:

1.在jar工程下启动
把jar工程打成jar包,通过java -jar timer.jar 运行

2.这web工程下启动
spring中我们可以通过实现接口ApplicationListener,并重写public void onApplicationEvent(ApplicationEvent event) {}可以在容器初始话的时候执行这个方法
下面展示下web工程下每天00:00执行任务的代码:
@Component
public class SystemInitListener implements ApplicationListener<ContextRefreshedEvent> { 
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		//创建定时器
		Timer timer = new Timer();
		Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,1);
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE),0,0,0);
        long timeInterval = 24 * 60 * 60 * 1000;
        timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				// 每天00:00需要做的事情				
			}
		}, calendar.getTime(), timeInterval);总结一下我使用过的4种类型的定时器:@Scheduled注解、quartz、new Timer().schedule、使用线程控制。
@Scheduled注解是最简单的方式,只需要启用定时器,在方法上添加注解即可。
在spring配置中加入:
<!-- 启用注解定时器 --> <task:annotation-driven />
在要具体的方法上加入注解@Scheduled
@Scheduled(cron = "0 0 * * * ? ")
    public void myTask(){
           //定时任务......
}quartz使用的是可配置的方式,将所有的定时器都配置再一个xml文件里面。
步骤如下:
1.创建一个spring的配置文件:spring-quartz.xml
2.定义工作任务的job
3.定义触发器Trigger并与job绑定
4.定义调度器,并将Trigger注册到scheduler
<bean id="myTask" class="cn.coolwind.MyTask"/> <!-- 1.定义工作任务job --> <bean id="testJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 定时器的类 --> <property name="targetObject" ref="myTask"></property> <!-- 需要定时执行的方法 --> <property name="targetMethod" value="test"></property> <property name="concurrent" value="false"></property> </bean> <!-- 2.定义触发器Trigger并与Job绑定 --> <bean id="testJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="testJob"/> <!-- 根据需要设置定时执行的时间 --> <property name="cronExpression" value="0 0/5 * * * ?" /> </bean> <!-- 3.定义调度器,并将trigger注册进去 --> <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="testJobTrigger" /> </list> </property> </bean>
最后记得将xml写入web.xml里!
<init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml, classpath:log4j.xml, classpath:spring-quartz.xml </param-value> </init-param>
使用Timer的schedule,schedule有3个参数:
schedule(TimerTask task, long delay, long period)
第一个为定时任务,根据业务需要重写TimerTask的run方法即可;
第二个为延时启动,单位毫秒;
第三个位多久运行一次,单位毫秒;
new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    //do Something
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },0,5L * 60 * 1000);使用线程来控制就更灵活一些,可以根据自己的需要判断什么时候运行,什么时候停止,这需要对java的线程有一定的了解。
public class TaskTest {
    private static final ExecutorService pool = Executors.newFixedThreadPool(5);// 线程池
    public static final TaskTest me = new TaskTest();
    public final int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; 
    public static void main(String[] args) {
        me.start();
    }
 
    private void start() {
        pool.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        for (int i = 0; i < arr.length; i++) {
                            if (1 == arr[i]) {
                                System.out.println("start!");
                                Thread.sleep(1*1000L);
                            }
                            if (6 == arr[i]) {
                                System.out.println("stop!");
                                Thread.sleep(5*1000L);
                            }
                            System.out.println(arr[i]);
                            if (9 == arr[i]) {
                                System.out.println("end!");
                                Thread.sleep(5*1000L);
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}以上是“java中Timer定时器怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。