先来个传统的Timer的例子:
package com.jerry.concurrency;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TraditionalTask {
public static void main(String[] args) throws ParseException {
Timer myTimer = new Timer();
myTimer.schedule(new Worker(), 1000);//1秒后执行
// 2012-02-28 09:58:00执行
myTimer.schedule(new Worker(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-02-28 09:58:00"));
myTimer.schedule(new Worker(), 5000,1000);//5秒后执行 每一秒执行一次
// 2012-02-28 09:58:00执行一次 以后每秒执行一次,如果设定的时间点在当前时间之前,任务会被马上执行,然后开始按照设定的周期定时执行任务
myTimer.schedule(new Worker(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-02-28 09:58:00"),1000);
myTimer.scheduleAtFixedRate(new Worker(), 5000,1000);//5秒后执行 每一秒执行一次 如果该任务因为某些原因(例如垃圾收集)而延迟执行,那么接下来的任务会尽可能的快速执行,以赶上特定的时间点
myTimer.scheduleAtFixedRate(new Worker(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-02-28 09:58:00"),1000);//和上个类似
}
}
class Worker extends TimerTask {
@Override
public void run() {
System.out.println("我被执行了!"+"时间是:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
传统的timer的缺点:Timer对任务的调度是基于绝对时间的;所有的TimerTask只有一个线程TimerThread来执行,因此同一时刻只有一个TimerTask在执行;任何一个TimerTask的执行异常都会导致Timer终止所有任务;由于基于绝对时间并且是单线程执行,因此在多个任务调度时,长时间执行的任务被执行后有可能导致短时间任务快速在短时间内被执行多次或者干脆丢弃多个任务。
ScheduledExecutorService克服了上述缺点,例子如下:
package com.jerry.concurrency;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TestScheduledExecutorService{
public static void main(String[] args) throws Exception{
ScheduledExecutorService execService = Executors.newScheduledThreadPool(3);
// 5秒后开始执行 每个2秒执行一次,如果有的任务执行要花费比其周期更长的时间,则将推迟后续执行,但不会同时执行
// 每次相隔相同的时间执行任务,如果任务的执行时间比周期还长,那么下一个任务将立即执行
execService.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println("任务:"+Thread.currentThread().getName()+" 执行了,时间为: "+System.currentTimeMillis());
try {
Thread.sleep(1000L);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 5, 2, TimeUnit.SECONDS);
//5秒后开始执行 每个2秒执行一次,保证固定的延迟为2秒 下一个任务的开始时间与上一个任务的结束时间间隔相同
execService.scheduleWithFixedDelay(new Runnable() {
public void run() {
System.out.println("任务:"+Thread.currentThread().getName()+"执行了,时间为:"+System.currentTimeMillis());
try {
Thread.sleep(1000L);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 5, 2, TimeUnit.SECONDS);
Thread.sleep(10000L);
execService.shutdown();
}
}
分享到:
相关推荐
ScheduledExecutorService是Java并发编程中一个非常重要的工具类,它属于ExecutorService接口的一个实现,主要用于执行定时或周期性的任务。这个服务提供了强大的定时任务管理能力,可以用来安排在未来某一时刻或者...
这是一个jar,很秀。自定义的一个Java定时器工具类。主要是结合Spring Boot一起使用,并在Spring Boot启动的时候一起启动运行。
ScheduledExecutorService 任务定时代码示例 ScheduledExecutorService 是 Java 中的并发编程 API,用于执行延迟或周期性的任务。下面是对 ScheduledExecutorService 的任务定时代码示例的详细解释。 ...
基于ScheduledExecutorService的两种方法详解 基于ScheduledExecutorService的两种方法是Java并发编程中的一种常见方法,用于实现定时任务的执行。ScheduledExecutorService是Java并发编程中的一种高级API,提供了...
首先,Java提供了多种方式来实现延迟执行,例如使用`java.util.Timer`类、`java.util.concurrent.DelayQueue`或者`java.util.concurrent.ScheduledExecutorService`。这里我们将重点讨论`ScheduledExecutorService`...
您可以在工作线程上运行任务(Runnable或Callable),以指定执行计划(如ScheduledExecutorService),并指定执行时间。 介面 ScheduledFuture<?> schedule( Runnable command, int executeTime, long delay,...
本实例将深入探讨如何使用`ScheduledExecutorService`接口来实现这一功能。`ScheduledExecutorService`是Java并发包`java.util.concurrent`中的一个接口,它提供了延迟执行和周期性执行任务的能力。 首先,我们需要...
Java通用任务执行程序,基于java.util.concurrent.ScheduledExecutorService。 Java通用的任务执行器,基于java.util.concurrent.ScheduledExecutorService工具类实现。 Maven: <groupId>...
而 `ScheduledExecutorService` 是 `ExecutorService` 的子接口,增加了定时及周期性任务的执行能力。 在提供的代码示例中,`BeeperControl` 类展示了如何使用 `ScheduledExecutorService` 来实现定时任务调度。...
ScheduledExecutorService 的测试实用程序实现 允许测试如下代码: class Foo { private int count = 0; public Foo(ScheduledExecutorService service, Bar bar) { service.scheduleAtFixedRate(new Runnable...
在Java中,有两种主要的定时器类:`java.util.Timer` 和 `java.util.concurrent.ScheduledExecutorService`。本文将详细讲解这两种定时器的用法、优缺点以及如何在实际项目中应用它们。 ### 1. `java.util.Timer` ...
Java提供了多种实现定时任务的机制,这里主要介绍两种:Java.util.Timer类和java.util.concurrent.ScheduledExecutorService接口。 1. Java.util.Timer类: Timer类是Java早期提供的定时任务工具,它可以安排在...
这通常是通过Java中的`java.util.Timer`类或者`java.util.concurrent.ScheduledExecutorService`来实现的。这两个工具提供了不同的功能和使用场景,让我们一一进行深入探讨。 首先,我们来看`java.util.Timer`类。...
在Java中,我们可以使用多种库和工具来创建和管理定时任务,如Java的内置API `java.util.Timer` 和 `java.util.concurrent.ScheduledExecutorService`。这两个类提供了丰富的功能,帮助开发者在特定时间或周期性地...
在Java中,我们可以使用多种库来实现这个功能,其中最常用的两个是`java.util.Timer`和`java.util.concurrent.ScheduledExecutorService`。 ## 1. `java.util.Timer` `Timer`类是Java标准库中的一个基础定时器,...
在Java中,有多种方式可以实现任务调度,包括基础的`Timer`类、`ScheduledExecutorService`接口以及第三方库如Quartz和JCronTab。 首先,我们来看`Timer`类。`Timer`是最简单的任务调度实现,它基于单线程模型,...
我们可以使用`Executors`类的静态方法创建一个ScheduledExecutorService实例,然后通过`schedule()`, `scheduleAtFixedRate()` 或 `scheduleWithFixedDelay()` 方法来安排任务。ScheduledExecutorService相比Timer有...
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3); ``` 然后,我们可以通过ScheduledExecutorService的scheduleWithFixedDelay()或scheduleAtFixedRate()方法来安排任务...
本示例将详细介绍如何在Android中使用两种主要的线程池:ThreadPoolExecutor和ScheduledExecutorService。 ThreadPoolExecutor是Java并发库中提供的一个基础线程池实现,它允许开发者自定义核心线程数、最大线程数...