保定php网站制作,苏州优化费用,佛山中谦建设网站,株洲在线论坛伴你生活每一天文章目录 ScheduledThreadPoolExecutortimer#xff08;不建议用#xff09;ScheduledThreadPoolExecutor处理异常应用 ScheduledThreadPoolExecutor
timer#xff08;不建议用#xff09;
timer也可以进行延迟运行#xff0c;但是会有很多问题。
比如task1运行时间超过… 文章目录 ScheduledThreadPoolExecutortimer不建议用ScheduledThreadPoolExecutor处理异常应用 ScheduledThreadPoolExecutor
timer不建议用
timer也可以进行延迟运行但是会有很多问题。
比如task1运行时间超过task2延迟时间。
Slf4j(topic c.Main)
public class Main {public static void main(String[] args) {Timer timer new Timer();TimerTask task1 new TimerTask() {Overridepublic void run() {log.debug(task 1);try {sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}};TimerTask task2 new TimerTask() {Overridepublic void run() {log.debug(task 2);}};log.debug(start...);timer.schedule(task1, 1000);timer.schedule(task2, 1000);}
}当task1出异常task2不会运行。
ScheduledThreadPoolExecutor
Executors就可以创建。
延迟执行
Slf4j(topic c.Main)
public class Main {public static void main(String[] args) {ScheduledExecutorService scheduledExecutorService Executors.newScheduledThreadPool(1);scheduledExecutorService.schedule(() - {log.debug(task1);int i 1 / 0; // 模拟异常}, 1, TimeUnit.SECONDS);scheduledExecutorService.schedule(() - {log.debug(task2);}, 1, TimeUnit.SECONDS);}
}可以看出通过控制线程数可以解决timer的缺点。 同时出现异常也不会影响其他任务运行。
定时运行
Slf4j(topic c.Main)
public class Main {public static void main(String[] args) {ScheduledExecutorService scheduledExecutorService Executors.newScheduledThreadPool(1);log.debug(start...);scheduledExecutorService.scheduleAtFixedRate(() - {log.debug(running...);}, 1, 1, TimeUnit.SECONDS); // 每隔1s反复执行}
}处理异常
try catch
ExecutorService pool Executors.newFixedThreadPool(1);
pool.submit(() - {try {log.debug(task1);int i 1 / 0;} catch (Exception e) {log.error(error:, e);}
});通过返回值判断
ExecutorService pool Executors.newFixedThreadPool(1);FutureBoolean f pool.submit(() - {log.debug(task1);int i 1 / 0; // 模拟异常return true;
});
log.debug(result:{}, f.get());若正常执行返回true 若出现异常get会返回异常信息。
应用
每周四定时运行
public class Main {public static void main(String[] args) throws ExecutionException, InterruptedException {// 获得当前时间LocalDateTime now LocalDateTime.now();// 获取本周四 18点时间LocalDateTime startTime now.with(DayOfWeek.THURSDAY).withHour(18).withMinute(0).withSecond(0).withNano(0);// 如果当前时间已经超过 本周四 那么找下周四if(now.compareTo(startTime) 0) {startTime startTime.plusWeeks(1);}// 计算时间差延时执行时间long initialDelay Duration.between(now, startTime).toMillis();// 执行间隔 1周long period 1000 * 60 * 60 * 24 * 7;ScheduledExecutorService executor Executors.newScheduledThreadPool(2);executor.scheduleAtFixedRate(() - {System.out.println(running..);}, initialDelay, period, TimeUnit.MILLISECONDS);}
}
文章转载自: http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.bysey.com.gov.cn.bysey.com http://www.morning.lpmlx.cn.gov.cn.lpmlx.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.phzrq.cn.gov.cn.phzrq.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.ktcfl.cn.gov.cn.ktcfl.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.mfjfh.cn.gov.cn.mfjfh.cn http://www.morning.xqjh.cn.gov.cn.xqjh.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.wqjpl.cn.gov.cn.wqjpl.cn http://www.morning.xkyqq.cn.gov.cn.xkyqq.cn http://www.morning.trfh.cn.gov.cn.trfh.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.rnzgf.cn.gov.cn.rnzgf.cn http://www.morning.c7622.cn.gov.cn.c7622.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.mfjfh.cn.gov.cn.mfjfh.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.lxwjx.cn.gov.cn.lxwjx.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.wwxg.cn.gov.cn.wwxg.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.xflzm.cn.gov.cn.xflzm.cn http://www.morning.dzfwb.cn.gov.cn.dzfwb.cn http://www.morning.gyqnp.cn.gov.cn.gyqnp.cn http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.xfncq.cn.gov.cn.xfncq.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.jwlmm.cn.gov.cn.jwlmm.cn http://www.morning.yxlpj.cn.gov.cn.yxlpj.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn http://www.morning.pgcmz.cn.gov.cn.pgcmz.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.shyqcgw.cn.gov.cn.shyqcgw.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.hmbxd.cn.gov.cn.hmbxd.cn http://www.morning.slqzb.cn.gov.cn.slqzb.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.xbnkm.cn.gov.cn.xbnkm.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.rcrnw.cn.gov.cn.rcrnw.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.prqdr.cn.gov.cn.prqdr.cn