目前用的java几_java目前常用的几种定时任务

java目前常用的几种定时任务

一、JDK自带的Timer

Timer是jdk中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次。

TimerTask是一个实现了Runnable接口的抽象类,代表一个可以被Timer执行的任务。

1 importorg.junit.Test;2 importjava.util.Timer;3 importjava.util.TimerTask;4

5 public classTimerTest {6 @Test7 public voidtest(){8 Timer timer = newTimer();9 TimerTask timerTask = newTimerTask() {10 @Override11 public voidrun() {12 System.out.println("当前时间");//具体的任务

13 }14 };15 timer.schedule(timerTask1,1000,1000);//任务,开始时间ms,间隔ms

16 System.out.println("start");17 try{18 Thread.sleep(2000);//不停两秒,显示不出来,因为test情况下当前用户线程结束,可是定时任务1s还没开始,就被终止

19 }catch(Exception e) {20

21 }22 System.out.println("end");23 }24 }

终止timer的方式

调用timer的cancle方法,

把timer线程设置成daemon线程,(new Timer(true)创建daemon线程),在jvm里,如果所有用户线程结束,那么守护线程也会被终止,不过这种方法一般不用。

当所有任务执行结束后,删除对应timer对象的引用,线程也会被终止。

调用System.exit方法终止程序

注意点

每一个Timer仅对应唯一一个线程。

Timer不保证任务执行的十分精确, schedule如果某一次调度时间比较长,那么后面的时间会顺延和scheduleAtFixedRate(严格按照调度时间来的,如果某次调度时间太长了,那么会通过缩短间隔的方式保证下一次调度在预定时间执行)会有不同的时间差。

Timer类的线程安全的

jdk1.5之后,ScheduledExecutorService代替了Timer来实现任务调度,加入了线程池等特性。

二、spring的Task

Task底层的实现,使用的ScheduledExecutorService。

2.1、注解的形式

使用@EnableScheduling启动定时任务注解解析,之后@Schedule写在执行的任务上即可

2.2、直接代码的形式

1 @EnableScheduling2 public classTask {3

4 @Scheduled(zone = "Asia/Beijing",cron = "0/10 * * * * * *")//zone表示时区

5 public voidschedule4(){6 }7 /*

8 fixedDelay对应的fixedDelayString支持字符串形式、占位符${}|#{}9 */

10 @Scheduled(fixedDelay = 5000)//上一次执行完毕时间点之后多长时间再执行

11 public voidschedule1(){12 }13 @Scheduled(fixedRate = 5000)//上一次开始执行时间点之后多长时间再执行

14 public voidschedule2(){15 }16 @Scheduled(initialDelay = 5000)//第一次延迟多长时间后再执行

17 public voidschedule3(){18 }19

20 }

TaskScheduler的子类

ConcurrentTaskScheduler:以当前线程执行任务。如果任务简单,可以直接使用这个类来执行。快捷方便。

DefaultManagedTaskScheduler:以当前线程执行任务,这是ConcurrentTaskScheduler的子类,添加了JNDI的支持。和ConcurrentTaskScheduler一样的用法,需要使用JNDI可以单独设置

ThreadPoolTaskScheduler:TaskScheduler接口的默认实现类,多线程定时任务执行。可以设置执行线程池数(默认一个线程),使用前必须得先调用initialize()【初始化方法】,有shutDown()方法,执行完后可以关闭线程

TimerManagerTaskScheduler:用于包装CommonJ中的TimerManager接口。在使用CommonJ进行调度时使用。(没有使用过)

三、Quartz

四、elastic-job分布式定时任务

基于Zookepper和Quartz开发,并且开源的Java分布式定时任务,解决Quartz不支持分布式的弊端,Elastic-Job采用分片的方式,是分布式调度解决方案。适用场景是:相对于流程比较简单,但是任务可以拆分到多个线程去执行。 每个任务都使用独立的线程池。

1 //注册zookeeper

2 @Bean(initMethod = "init")3 public ZookeeperRegistryCenter regCenter( final String serverList,finalString namespace) {4 return new ZookeeperRegistryCenter(newZookeeperConfiguration(serverList, namespace));5 }

1 //配置任务config

2 @Configuration3 public classLoginOverdueJobConfig {4 @Resource5 privateZookeeperRegistryCenter regCenter;6

7 @Resource8 privateJobEventConfiguration jobEventConfiguration;9

10 private LiteJobConfiguration getLiteJobConfiguration(final Class extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, finalString shardingItemParameters) {11 return LiteJobConfiguration.newBuilder(newSimpleJobConfiguration(JobCoreConfiguration.newBuilder(12 jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).build(), jobClass.getCanonicalName())).overwrite(true).build();13 }14

15 @Bean(initMethod = "init")16 public JobScheduler simpleCloudStorageJobScheduler(final LoginOverdueJob loginOverdueJob, @Value("${simpleJob.cron}") final String cron, @Value("${simpleJob.shardingTotalCount}") final intshardingTotalCount,17 @Value("${simpleJob.shardingItemParameters}") finalString shardingItemParameters) {18 return newSpringJobScheduler(loginOverdueJob, regCenter, getLiteJobConfiguration(loginOverdueJob.getClass(), cron, shardingTotalCount, shardingItemParameters), jobEventConfiguration);19 }20 }

1 //任务执行函数

2 @Component3 public class LoginOverdueJob implementsSimpleJob {4 public voidexecute(ShardingContext shardingContext) {5

6 }7 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值