java定时器

1.Timer 和TimerTask的使用

Timer t = new Timer();

TimerTaskE task = new TimerTaskE();

t.schedule(task,Date d);


TimerTaskE extends TimerTask{

public void run(){

Syso("sssss");

}

}



2.schedule()方法解释

在特定时间执行任务,只执行一次
public void schedule(TimerTask task,Date time)
在特定时间之后执行任务,只执行一次
public void schedule(TimerTask task, long delay)
指定第一次执行的时间,然后按照间隔时间,重复执行
public void schedule(TimerTask task, Date firstTime, long period)
在特定延迟之后第一次执行,然后按照间隔时间,重复执行

3.自定义定时器


4.spring定时器

(1)使用Timer处理

public class MainTask extends TimerTask{
	@Override
	public void run() {
		System.out.println("检测用户是否掉线");
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	<bean id="mainTask" class="org.time.MainTask"></bean>

	<!-- 注册定时器信息 -->
	<bean id="springTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
		<!-- 延迟1秒执行首次任务 -->
		<property name="delay" value="1000"></property>
		<!-- 每隔2秒执行一次任务 -->
		<property name="period" value="2000"></property>
		<!-- 具体执行的任务 -->
		<property name="timerTask" ref="mainTask"></property>
	</bean>
	<!-- 配置任务调度器工厂 -->
	<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
		<property name="scheduledTimerTasks">
			<list>
				<ref bean="springTask"/>
			</list>
		</property>
	</bean>
</beans>
(2)使用quartz

spring-context.jar和quartz.jar包

定时器类不需要继承自任何类,自己写个类,写个执行方法即可

<?xml version="1.0" encoding="GBK"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"  
    default-autowire="byName">   
    <bean id="helloWorker" class="com.jd.service.meeting.worker.MyWorker"></bean>  
    <bean id="startQuertz" lazy-init="false" autowire="no"  
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
                <bean class="org.springframework.scheduling.quartz.CronTriggerBean">  
                    <property name="jobDetail">  
                        <bean  
                            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
                            <property name="concurrent">  
                                <value>false</value>  
                            </property>  
                            <property name="targetObject">  
                                <ref bean="helloWorker" />  
                            </property>  
                            <property name="targetMethod">  
                                <value>running</value>  
                            </property>  
                        </bean>  
                    </property>  
                    <property name="cronExpression">  
                        <value>0/10 * * * * ?</value>  
                    </property>  
                </bean>   
                  
            </list>  
        </property>  
    </bean>  
</beans>  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值