昭通市网站建设_网站建设公司_UX设计_seo优化
2026/1/14 21:17:02 网站建设 项目流程

一、常用方法

  • schedule

特性说明
✅ 单次执行schedule仅执行一次,不同于scheduleAtFixedRatescheduleWithFixedDelay的周期性执行。
✅ 异步执行任务在后台线程池中执行,不会阻塞调用线程。
✅ 支持返回值与异常使用Callable可获取结果或捕获异常(通过future.get())。
✅ 可取消在任务执行前可调用cancel()取消。
❌ 不保证精确时间实际执行时间受系统负载、线程调度等因素影响,只是近似延迟。
❌ 不自动重试若任务抛出异常,不会重试(与周期任务不同)。
private static void schedule(ScheduledExecutorService service, final int sleepTime){ service.schedule(new Runnable() { @Override public void run() { long start = new Date().getTime(); System.out.println("schedule 开始执行时间 " + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } long end = new Date().getTime(); System.out.println("schedule 执行花费时间= " + (end - start) / 1000+"s"); System.out.println("schedule 结束执行时间 " + DateFormat.getTimeInstance().format(new Date())); System.out.println(">>>>>>>>>>>>>>>>>>>"); } },5,TimeUnit.SECONDS); }

结论:只会执行一次,比较简单

  • scheduleAtFixedRate

延迟固定时间频率执行任务

任务执行耗时3s,period间隔为5s

scheduleAtFixedRate(service, 3000); private static void scheduleAtFixedRate(ScheduledExecutorService service, final int sleepTime) { service.scheduleAtFixedRate(new Runnable() { public void run() { long start = new Date().getTime(); System.out.println("scheduleAtFixedRate 开始执行时间 " + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // long end = System.currentTimeMillis(); long end = new Date().getTime(); System.out.println("scheduleAtFixedRate 执行花费时间= " + (end - start) / 1000); System.out.println("scheduleAtFixedRate 结束执行时间 " + DateFormat.getTimeInstance().format(new Date())); System.out.println(">>>>>>>>>>>>>>>>>>>"); } }, 1000, 5000, TimeUnit.MILLISECONDS); }

场景一:任务执行耗时时长小于period间隔,任务执行完成后,会等到了延迟时间,再执行下一次任务,本次任务开始时间+延迟时间间隔=下次任务开始时间

场景二:任务执行耗时时长大于period间隔,任务执行耗时5秒,period间隔为3秒,本次任务完成后,下次任务启动会立即执行.

scheduleAtFixedRate(service, 5000); private static void scheduleAtFixedRate(ScheduledExecutorService service, final int sleepTime) { service.scheduleAtFixedRate(new Runnable() { public void run() { long start = new Date().getTime(); System.out.println("scheduleAtFixedRate 开始执行时间 " + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // long end = System.currentTimeMillis(); long end = new Date().getTime(); System.out.println("scheduleAtFixedRate 执行花费时间= " + (end - start) / 1000+"s"); System.out.println("scheduleAtFixedRate 结束执行时间 " + DateFormat.getTimeInstance().format(new Date())); System.out.println(">>>>>>>>>>>>>>>>>>>"); } }, 1000, 3000, TimeUnit.MILLISECONDS); }

  • ​​​​​scheduleWithFixedDelay

scheduleWithFixedDelay(service, 2000); private static void scheduleWithFixedDelay(ScheduledExecutorService service, final int sleepTime) { service.scheduleWithFixedDelay(new Runnable() { public void run() { long start = new Date().getTime(); System.out.println("scheduleWithFixedDelay 开始执行时间:" + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } long end = new Date().getTime(); System.out.println("scheduleWithFixedDelay执行花费时间=" + (end - start) / 1000 + "s"); System.out.println("scheduleWithFixedDelay执行完成时间:" + DateFormat.getTimeInstance().format(new Date())); System.out.println("======================================"); } }, 1000, 3000, TimeUnit.MILLISECONDS); }

场景一:任务执行耗时小于period间隔,任务执行耗时2秒,period间隔为3秒

结论:本次任务执行结束时间+period=下次任务开始时间

scheduleWithFixedDelay(service, 5000); private static void scheduleWithFixedDelay(ScheduledExecutorService service, final int sleepTime) { service.scheduleWithFixedDelay(new Runnable() { public void run() { long start = new Date().getTime(); System.out.println("scheduleWithFixedDelay 开始执行时间:" + DateFormat.getTimeInstance().format(new Date())); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } long end = new Date().getTime(); System.out.println("scheduleWithFixedDelay执行花费时间=" + (end - start) / 1000 + "s"); System.out.println("scheduleWithFixedDelay执行完成时间:" + DateFormat.getTimeInstance().format(new Date())); System.out.println("======================================"); } }, 1000, 3000, TimeUnit.MILLISECONDS); }

场景一:任务执行耗时大于period间隔,任务执行耗时5秒,period间隔为3秒

结论:本次任务执行结束时间+period=下次任务开始时间,delay是计算上一个任务执行结束的时间和本次任务开始时间的差值,此值和任务的执行时间就没有关系了

特性scheduleAtFixedRatescheduleWithFixedDelay
间隔基准上次开始时间上次结束时间
适用场景需要严格频率(如每5秒上报一次)需要固定空闲间隔(如每次处理完等3秒再处理)
任务超时影响可能导致任务“追赶”甚至连续执行始终保证任务间有 delay 间隔

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询