魔术快斗,Java定时任务-手机188体育_188体育app|下载

国际新闻 183℃ 0

Java开发进程中经常会遇到运用守时使命的状况,我总结了一下有如下四种猜成语办法:Timer、ScheduledExecutorService、SpringTask、Quartz。

一、运用java的Timer

1、Timer

new Timer("testTimer").schedule(new TimerTask() {
@Override
public void run() {
System.out.println("TimerTask");
}
}, 1000,2000);

解说:1000ms是推迟发动时刻,2000ms是守时使命周期,每2s履行一次

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try {
Date date = dateFormat.parse("2018-07-11 12:00:00.000");
new Timer("testTimer1").scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.println("Tim澳洲留学中介erTask");
}
}, date,2000);
} catch (ParseException e) {
e.printStackTrace();
}

解说:date是开端时刻,2000ms是守时使命周期,每2s履行戏法快斗,Java守时使命-手机188体育_188体育app|下载一次

timer有2中办法schedule和scheduleAtFixe戏法快斗,Java守时使命-手机188体育_188体育app|下载dRate,前者会等使命完毕在开端核算时刻距离,后者是在使命开端就核算时刻,有并发的状况

二、使戏法快斗,Java守时使命-手机188体育_188体育app|下载用ScheduledExecutorService

1、ScheduledExecutorService

scheduledExecutorService.schedule(new Runnable() {
@Override
public voi戏法快斗,Java守时使命-手机188体育_188体育app|下载d run() {
System.out.println("ScheduledTask");
}
},1, TimeUnit.SECONDS);

解说:推迟1s发动,履行一次

ScheduledExecuto吴平月rService scheduledEx考ecutorService = Executors.newScheduledThreadPool(10);
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Overri王碧含de
public void run() {
System.out.println("ScheduledTask");
}
}, 1, 1, TimeUnit.SECONDS);

解说:推迟1s发动,每隔1s履行一次,是前一个使命开端时就开端核算时刻距离,可是会等上一个使命完毕在开端下一个

ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
@Override
public v拆鹿迪小说oid run() {
System.out.println("ScheduledTask");
}
}, 1, 1, TimeUnit.SECONDS);

解说:推迟1s发动,在前一个使命履行完结之后,推迟1s在履行

三、运用SpringTask

1、写使命类

package com.zb.timedtask;
import com.zb.controller.StudentController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.sprin戏法快斗,Java守时使命-手机188体育_188体育app|下载gframework.stereotype.Service;
@Service
public class SpringTask {
private static final Logger log = LoggerFactory.getLogger(SpringTask.class);
@Scheduled(cron = "1/5 * * * * *")
public void task1(){
log.info("springtask 守时使命!");
}

@Scheduled(最管用的收惊办法initialDelay = 1000,fixedRate = 1*1000)
publ双皮奶做法ic void task戏法快斗,Java守时使命-手机188体育_188体育app|下载2()周口天气预报15天{
log.info("springtask 守时使命!");
}
}

解说:

task1是每隔5s履行一次,{秒} {分} {时} {日期} {月} {星期}

task2是推迟1s,每隔1S履行一次

2、装备文件修正

(1)简略版


(2)使命池版




(3)解说

假设只要一个守时使命,能够用简略版;如果有多个守时使命,则要用使命池,否则它会次序履行。

两个使命的时刻距离为:履行时刻+设定的守时刻隔

比方:(这个使命8s履行一次)

@Scheduled(cron = "1越王勾践剑/4 * * * * *")
public void task2(){
log.info("springtask傲慢与偏见读后感 守时使命2!");
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

四、运用Quartz结构

1、加依靠



org.quartz-scheduler
quartz
2.3.0



org.spring海派医药有限公司framework
spring-context-support
4.3.4.RELEASE

2、Job完成

package com.zb.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class HelloWorldJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
String strTime = new SimpleDateFormat("HH-mm-ss").format(new Date());
System.out.println( strTime + ":Hello World!");
}
}

3、调度器(能够用listener在项目发动时履行)

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class MyScheduler {
public static void main(String[] args) throws SchedulerException {
//创立调度器Schedule
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
//创立JobDetail实例,并与HelloWordlJob类绑定
JobDetail jobDetail = JobBuilder.newJob(HelloWorldJob.class).withIdentity("job1", "jobGroup1")
.build();黎美言
//创立触发器Trigger实例(当即履行,每隔1S履行一次)
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "triggerGroup1")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever())
.build();
//开端履行
scheduler.甜甜圈scheduleJob(jobDetail, trigger);
scheduler.start();
}
}

解说:上面用的是简略触发器,也能够用Con触发器,如下

Trigger cronTrigger = TriggerBuilder.newTrigger()
.withIdentity("trigger2", "triggerGroup2")
.startNow()
.withSchedule(cronSchedule("0 42 10 * * ?"))
.build();

4、整合spring

也能够直接把上面的调度器写成装备文件,整合spring

(1)job

package com.zb.quartz;
import java.text.SimpleDateFormat;
import java.util.Date;
public class QuarFirstJob {
public void first() {
String strTime = new SimpleDateFormat("HH-mm-gakkiss").format(new Dat十大废物食物e());
System.out.println( strTime + ":Hello World!");
}
}

(2)装备文件



class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">








first




class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">






0/5 * * * * ?




c看书假文雅lass="org.springframework.scheduling.quartz.SchedulerFactoryBean">






5、时刻

public class QuarFirstJob {
public void first() {
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
e.printStackTrace();
}
String strTime = new SimpleDateFormat("HH-徐志摩的诗mm-ss").format(new Da戏法快斗,Java守时使命-手机188体育_188体育app|下载te());
System.out.println( strTime + ":Hello World!");
}
}

上面的装备里边写是5s距离,把上面的sleep时刻别离改成4和6,发现两次使命距离是履行时刻和距离时刻的最大值,别离是5,6

总结

Quartz是履行时刻和距离时刻的最大值(比方;履行时刻是3s,距离是2s,则每3s履行一次;履行时刻是3s,距离是5s,则每5s履行一次。)

Spring task是履行时刻+距离时刻(比方;履行时刻是3s,距离是2s,则每5s履行一次;履行时刻是3s,距离是5s,则每8s履行一次。)

timer有2中办法sch海伦凯勒edule和scheduleAtFixedRate,前者会等使命完毕在开端核算时刻距离,后者是在使命开端就核算时刻,有并发的状况

ScheduledExecutorService的scheduleAtFixedRate相似Quartz,scheduleWithFixedDelay相似SpringTask