阜阳专业网站建设区块链网站开发
阜阳专业网站建设,区块链网站开发,计算机应用技术毕业设计,竣工验收全国公示平台本文列举常见的java定时任务实现方式#xff0c;并做一定比较。1. 循环内部sleep实现周期执行创建一个thread#xff0c;run() while循环里sleep()来实现周期性执行; 简单粗暴#xff0c;作为一个初学者很容易想到。public class Task1 {public static void main(String[] a…本文列举常见的java定时任务实现方式并做一定比较。1. 循环内部sleep实现周期执行创建一个threadrun() while循环里sleep()来实现周期性执行; 简单粗暴作为一个初学者很容易想到。public class Task1 {public static void main(String[] args) {// run in a secondfinal long timeInterval 1000;Runnable runnable new Runnable() {public void run() {while (true) {System.out.println(Hello !!);// 使用线程休眠来实现周期执行,try {Thread.sleep(timeInterval);} catch (InterruptedException e) {e.printStackTrace();}}}};Thread thread new Thread(runnable);thread.start();}}2. 使用Timer类调度TimerTask任务改进当启动和去取消任务时可以控制; 第一次执行任务时可以指定你想要的delay时间不足Timer的调度是基于绝对时间的所以当系统时间改变时会影响Timer。Timer只有一个工作线程所以当一个任务执行时间很长的时候会影响后续任务的调度。而ScheduledThreadPoolExecutor通过线程池的方式配置更灵活。如果任务抛出了一个未检查的异常将会导致Timer的工作线程被终止使Timer无法在继续运行。import java.util.Timer;import java.util.TimerTask;public class HelperTest {public static void main(String[] args) {// 具体任务。TimerTask task new TimerTask() {Overridepublic void run() {// task to run goes hereSystem.out.println(Hello !!!);}};// Timer类可以调度任务。 Timer实例可以调度多任务它是线程安全的。Timer timer new Timer();long delay 0;long intevalPeriod 1 * 1000;// schedules the task to be run in an intervaltimer.scheduleAtFixedRate(task, delay, intevalPeriod);}}3. 使用j.u.c.ScheduledExecutorService定时任务接口相比于Timer的单线程它是通过线程池的方式来执行任务的可以灵活的设定第一次执行任务delay时间提供了良好的约定以便设定执行的时间间隔import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class Task3 {public static void main(String[] args) {ScheduledExecutorService service new ScheduledThreadPoolExecutor(1);// 初始化延迟0ms开始执行每隔200ms重新执行一次任务。ScheduledExecutorService pool new ScheduledThreadPoolExecutor(1);pool.scheduleAtFixedRate(new Runnable() {Overridepublic void run() {// task to run goes hereSystem.out.println(Hello !);}}, 0, 200L, TimeUnit.MILLISECONDS);}实现类使用的是ScheduledThreadPoolExecutor。该类继承自ThreadPoolExecutor read more阻塞队列使用的是DelayedWorkQueue是ScheduledThreadPoolExecutor的内部类。ScheduledExecutorService接口方法说明其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比较方便。scheduleAtFixedRate(runnable, 0, 200L, TimeUnit.MILLISECONDS) 按指定周期执行某个任务初始化延迟0ms开始执行每隔200ms重新执行一次任务。scheduleWithFixedDelay(runnable, 0, 200L, TimeUnit.MILLISECONDS) 按指定间隔执行某个任务初始化时延时0ms开始执行下次执行时间是(本次执行结束 延迟200ms)后开始执行。schedule(Runnable command, long delay, TimeUnit unit) 在delay延时后执行一次性任务备注对于scheduleAtFixedRate实际上如果当前线程阻塞执行时间t 设置的间隔时间period下次是在t时间后执行并非period时间后立即开始。ScheduledExecutorService的spring配置 spring.xml xxx.javaAutowiredQualifier(gkHeartBeatScheduler)ScheduledExecutorService scheduledExecutorService;scheduledExecutorService.scheduleAtFixedRate(new Runnable() {Overridepublic void run() {System.out.println(do sth);}}, 1l, 2l, TimeUnit.SECONDS);spring ScheduledExecutorFactoryBean内部同样使用的ScheduledThreadPoolExecutor并对其做了包装处理。public class ScheduledExecutorFactoryBean extends ExecutorConfigurationSupport implements FactoryBean4. Sheduled注解方式Sheduled内部也使用了ScheduledThreadPoolExecutor。具体源代码可参见spring-context包中的ScheduledAnnotationBeanPostProcessor。用法就很简单了举例pom文件引入spring-context依赖使用注解方式配置定时任务即可import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;ComponentEnableSchedulingpublic class ScheduledAnnotationDemo {// Scheduled和触发器元素一起添加到方法上.Scheduled(fixedDelay5000)public void doSomething() {System.out.println(like scheduleWithFixedDelay);}Scheduled(fixedRate5000)public void doSomething() {System.out.println(like scheduleAtFixedRate);}// fixed-delay、fixed-rate任务都可以设置初始delay。Scheduled(initialDelay1000, fixedRate5000)public void doSomething() {// something that should execute periodically}// 也支持cron表达式Scheduled(cron 0/5 * * * * ?)public void doSomething() {// something that should execute on weekdays onlySystem.out.println(5s执行一次);}//cron举例:(秒 - 分 - 时 - 日 - 月- 星期)// */5 * * * * ? 每隔5秒执行一次// 0 */1 * * * ? 每隔1分钟执行一次// 0 0 1 * * ? 每天1点执行一次// 0 0 1 1 * ? 每月1号1点执行一次// 0 0 1 L * ? 每月最后一天1点执行一次// 0 0 1 ? * L 每周星期天1点执行一次}上面使用EnableScheduling的方式启动定时任务等价于在spring xml中配置元素。5. 开源任务调度框架QuartzQuartz , 功能强大的任务调度库。适用于具有更复杂调度要求的场景。提供了对持久化任务调度信息、事务、分布式的支持。与spring无缝对接。6. 小结使用ScheduledThreadPoolExecutor完成简单定时任务是比较理想和常用的实现方式。书写时更容易理解其过程实现。也可以用Sheduled注解的形式更加轻量化看起来更简洁。对复杂的任务调度可以使用Quartz框架。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/91252.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!