Java实现定时任务_第1页
Java实现定时任务_第2页
Java实现定时任务_第3页
Java实现定时任务_第4页
Java实现定时任务_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第Java实现定时任务本文实例为大家分享了Java实现定时任务的具体代码,供大家参考,具体内容如下

1使用java.util.Timer

这种方式的定时任务主要用到两个类,Timer和TimerTask,使用起来比较简单。其中Timer负责设定TimerTask的起始与间隔执行时间。TimerTask是一个抽象类,new的时候实现自己的run方法,然后将其丢给Timer去执行即可。

代码示例:

importjava.time.LocalDateTime;

importjava.util.Timer;

importjava.util.TimerTask;

publicclassSchedule{

publicstaticvoidmain(String[]args){

TimerTasktimerTask=newTimerTask(){

@Override

publicvoidrun(){

System.out.println("当前线程:"+Thread.currentThread().getName()+"当前时间"+LocalDateTime.now());

}

};

//在指定延迟0毫秒后开始,随后地执行以2000毫秒间隔执行timerTask

newTimer().schedule(timerTask,0L,2000L);

System.out.println("当前线程:"+Thread.currentThread().getName()+"当前时间"+LocalDateTime.now());

}

}

缺点:

Timer的背后只有一个线程,不管有多少个任务,都只有一个工作线程串行执行,效率低下受限于单线程,如果第一个任务逻辑上死循环了,后续的任务一个都得不到执行依然是由于单线程,任一任务抛出异常后,整个Timer就会结束,后续任务全部都无法执行

2使用ScheduledExecutorService

ScheduledExecutorService即是Timer的替代者,JDK1.5并发包引入,是基于线程池设计的定时任务类。每个调度任务都会分配到线程池中的某一个线程去执行,任务就是并发调度执行的,任务之间互不影响。

Java5.0引入了java.util.concurrent包,其中的并发实用程序之一是ScheduledThreadPoolExecutor,它是一个线程池,用于以给定的速率或延迟重复执行任务。它实际上是Timer/TimerTask组合的更通用替代品,因为它允许多个服务线程,接受各种时间单位,并且不需要子类TimerTask(只需实现Runnable)。使用一个线程配置ScheduledThreadPoolExecutor使其等效于Timer。

代码示例:

importjava.time.LocalDateTime;

importjava.util.concurrent.*;

publicclassSchedule{

publicstaticvoidmain(String[]args){

//创建一个ScheduledThreadPoolExecutor线程池,核心线程数为5

ScheduledExecutorServicescheduledExecutorService=newScheduledThreadPoolExecutor(5);

//创建Runnable打印当前线程和当前时间

Runnabler=()-System.out.println("当前线程:"+Thread.currentThread().getName()+"当前时间"+LocalDateTime.now());

/**

*schedule:只执行一次调度

*scheduleAtFixedRate:一开始就计算间隔时间,如果任务超过间隔时间,那么就直接开始下一个任务

*scheduleWithFixedDelay:任务无论执行多久,都要等待上一轮任务完成之后再间隔指定时间,然后才开始下一个任务

*/

//在指定1秒延迟后执行r,之后每两秒执行一次

scheduledExecutorService.scheduleAtFixedRate(r,1,2,TimeUnit.SECONDS);

}

}

3使用SpringTask

SpringTask底层是基于JDK的ScheduledThreadPoolExecutor线程池来实现的。直接通过Spring提供的@Scheduled注解即可定义定时任务,非常方便。

以SpringBoot来作为示例,步骤为

1.在启动类所在包下创建Schedule类(在没有配置@ComponentScan的情况下,SpringBoot只会默认扫描启动类所在包的spring组件)

2.在该类上添加@Component和@EnableScheduling注解

3.在方法上添加@Scheduled注解,该注解主要参数如下

Stringcron()default"";

//支持cron表达式

longfixedDelay()default-1;

//在最后一次调用结束和下一次调用开始之间的时间间隔,以毫秒为单位

StringfixedDelayString()default"";

//同上,类似ScheduledExecutorService的scheduleWithFixedDelay

longfixedRate()default-1;

//在调用之前的时间间隔,以毫秒为单位

StringfixedRateString()default"";

//同上,类似ScheduledExecutorService的scheduleAtFixedRate

longinitialDelay()default-1;

//在第一次执行fixedRate()或fixedDelay()任务之前要延迟的毫秒数

StringinitialDelayString()default"";

//同上

代码示例:

importorg.springframework.scheduling.annotation.EnableScheduling;

importorg.springframework.scheduling.annotation.Scheduled;

importorg.springframework.stereotype.Component;

importjava.time.LocalDateTime;

@Component

@EnableScheduling

publicclassSchedule{

@Scheduled(fixedRate=2000L)

publicvoidtask(){

System.out.println("当前线程:"+Thread.currentThread().getName()+"当前时间"+LocalDateTime.now());

}

}

优点:简单,轻量,支持Cron表达式缺点:默认只支持单机,是单线程的,并且提供的功能比较单一

可以通过@EnableAsync和@Async开启多线程

importorg.springframework.scheduling.annotation.Async;

importorg.springframework.scheduling.annotation.EnableAsync;

importorg.springframework.scheduling.annotation.EnableScheduling;

importorg.springframework.scheduling.annotation.Scheduled;

importorg.springframework.stereotype.Component;

importjava.time.LocalDateTime;

@Component

@EnableAsync

//开启异步多线程

@EnableScheduling

publicclassSchedule{

@Async

@Scheduled(fixedRate=2000L)

publicvoidtask(){

System.out.println("当前线程:"+Thread.currentThread().getName()+"当前时间"+LocalDateTime.now());

}

}

使用@EnableAsync注解后,默认情况下,Spring将搜索关联的线程池定义:上下文中的唯一org.springframework.core.task.TaskExecutor

的bean,或者名为taskExecutor的java.util.concurrent.Executor

的bean。如果两者都无法解析,则将使用org.springframework.core.task.SimpleAsyncTaskExecutor来处理异步方法调用。

TaskExecutor实现为每个任务启动一个新线程,异步执行它。支持通过concurrencyLimitbean属性限制并发线程。默认情况下,并发线程数是无限的,所以使用默认的线程池有导致内存溢出的风险。

注意:刚才的运行结果看起来是线程复用的,而实际上此实现不重用线程!应尽量实现一个线程池TaskExecutor,特别是用于执行大量短期任务。不要使用默认的SimpleAsyncTaskExecutor。

importorg.springframework.context.annotation.Bean;

importorg.springframework.scheduling.annotation.Async;

importorg.springframework.scheduling.annotation.EnableAsync;

importorg.springframework.scheduling.annotation.EnableScheduling;

importorg.springframework.scheduling.annotation.Scheduled;

importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

importorg.springframework.stereotype.Component;

importjava.time.LocalDateTime;

importjava.util.concu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论