- 浏览: 980954 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
Executor接口的定义:http://donald-draper.iteye.com/blog/2365625
ExecutorService接口定义:http://donald-draper.iteye.com/blog/2365738
Future接口定义:http://donald-draper.iteye.com/blog/2365798
FutureTask解析:http://donald-draper.iteye.com/blog/2365980
CompletionService接口定义:http://donald-draper.iteye.com/blog/2366239
ExecutorCompletionService解析:http://donald-draper.iteye.com/blog/2366254
AbstractExecutorService解析:http://donald-draper.iteye.com/blog/2366348
//ScheduledFuture,继承了Delayed和Future接口
ExecutorService接口定义:http://donald-draper.iteye.com/blog/2365738
Future接口定义:http://donald-draper.iteye.com/blog/2365798
FutureTask解析:http://donald-draper.iteye.com/blog/2365980
CompletionService接口定义:http://donald-draper.iteye.com/blog/2366239
ExecutorCompletionService解析:http://donald-draper.iteye.com/blog/2366254
AbstractExecutorService解析:http://donald-draper.iteye.com/blog/2366348
package java.util.concurrent; import java.util.concurrent.atomic.*; import java.util.*; /** * An {@link ExecutorService} that can schedule commands to run after a given * delay, or to execute periodically. * ScheduledExecutorService可以在给定的时延后执行任务,或周期性、间隔性 执行任务。 * <p> The <tt>schedule</tt> methods create tasks with various delays * and return a task object that can be used to cancel or check * execution. The <tt>scheduleAtFixedRate</tt> and * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks * that run periodically until cancelled. * schedule可以执行指定时延的任务,返回一个ScheduledFuture,我们可以通过 ScheduledFuture取消或检查任务的执行。scheduleAtFixedRate和scheduleWithFixedDelay 方法可以创建和执行周期性的任务,直到任务取消 * <p> Commands submitted using the {@link Executor#execute} and * {@link ExecutorService} <tt>submit</tt> methods are scheduled with * a requested delay of zero. Zero and negative delays (but not * periods) are also allowed in <tt>schedule</tt> methods, and are * treated as requests for immediate execution. * 如果提交的任务延时为0,则提交的任务用Executor#execute方法,或ExecutorService的 submit方法执行任务。在schedule方法中,允许延时为0或负数,对于这种情况, 我们当立即执行任务的请求对待。 * <p>All <tt>schedule</tt> methods accept [i]relative[/i] delays and * periods as arguments, not absolute times or dates. It is a simple * matter to transform an absolute time represented as a {@link * java.util.Date} to the required form. For example, to schedule at * a certain future <tt>date</tt>, you can use: <tt>schedule(task, * date.getTime() - System.currentTimeMillis(), * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a * relative delay need not coincide with the current <tt>Date</tt> at * which the task is enabled due to network time synchronization * protocols, clock drift, or other factors. * schedule接受一个相对的延时时间和时间单元做为参数,非绝对的时间或日期。 我们可以将一个java.util.Date表示的绝对时间转化为需要的格式。比如想在一个 确定的时间执行,可以用schedule(task,date.getTime() - System.currentTimeMillis(),TimeUnit.MILLISECONDS)。 使用绝对时间的转换的时候,要注意相对的过期时间,由于服务器网络时间同步协议导致的时间偏移,不能在 准确的时间调度延时任务。 * The {@link Executors} class provides convenient factory methods for * the ScheduledExecutorService implementations provided in this package. * 在执行器Executors提供了便利的工厂方法,用于创建ScheduledExecutorService。 * <h3>Usage Example</h3> * * Here is a class with a method that sets up a ScheduledExecutorService * to beep every ten seconds for an hour: * 下面是一个在一小时内,每10调度一次周期任务的示例: * <pre> {@code * import static java.util.concurrent.TimeUnit.*; * class BeeperControl { * private final ScheduledExecutorService scheduler = * Executors.newScheduledThreadPool(1); * * public void beepForAnHour() { * final Runnable beeper = new Runnable() { * public void run() { System.out.println("beep"); } * }; * final ScheduledFuture<?> beeperHandle = //每10秒执行一次,周期任务 * scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); //一个小时后,取消周期任务 * scheduler.schedule(new Runnable() { * public void run() { beeperHandle.cancel(true); } * }, 60 * 60, SECONDS); * } * }}</pre> * * @since 1.5 * @author Doug Lea */ public interface ScheduledExecutorService extends ExecutorService { /** * Creates and executes a one-shot action that becomes enabled * after the given delay. 根据Runnable,delay创建一个只执行一次的延时无结果任务ScheduledFuture<?>,延时时间过后,执行无结果任务 * * @param command the task to execute * @param delay the time from now to delay execution * @param unit the time unit of the delay parameter * @return a ScheduledFuture representing pending completion of * the task and whose <tt>get()</tt> method will return * <tt>null</tt> upon completion * @throws RejectedExecutionException if the task cannot be * scheduled for execution * @throws NullPointerException if command is null */ public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); /** * Creates and executes a ScheduledFuture that becomes enabled after the * given delay. * 根据Callable,delay创建一个只执行一次的延时有结果任务ScheduledFuture<V>,延时时间过后,执行任务 * @param callable the function to execute * @param delay the time from now to delay execution * @param unit the time unit of the delay parameter * @return a ScheduledFuture that can be used to extract result or cancel * @throws RejectedExecutionException if the task cannot be * scheduled for execution * @throws NullPointerException if callable is null */ public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit); /** * Creates and executes a periodic action that becomes enabled first * after the given initial delay, and subsequently with the given * period; that is executions will commence after * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then * <tt>initialDelay + 2 * period</tt>, and so on. * If any execution of the task * encounters an exception, subsequent executions are suppressed. * Otherwise, the task will only terminate via cancellation or * termination of the executor. If any execution of this task * takes longer than its period, then subsequent executions * may start late, but will not concurrently execute. * 创建执行一个周期性间隔为period的任务,在初始化延时initialDelay后,执行任务, 然后在initialDelay+period执行任务,接着在initialDelay + 2 * period执行任务,依次类推。 如果任务一个任务执行异常,则后续的任务将会被取消。另外任务执行被结束通过取消或执行器执行结束。 如果周期性任务中的一个任务所花时间大于period,后续的任务执行将会延迟,而不是并发执行。 initialDelay为0,period为2,每个任务执行需要3秒 0--2--4--6--.... t0--t1--t2--t3--... * @param command the task to execute * @param initialDelay the time to delay first execution * @param period the period between successive executions * @param unit the time unit of the initialDelay and period parameters * @return a ScheduledFuture representing pending completion of * the task, and whose <tt>get()</tt> method will throw an * exception upon cancellation * @throws RejectedExecutionException if the task cannot be * scheduled for execution * @throws NullPointerException if command is null * @throws IllegalArgumentException if period less than or equal to zero */ public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); /** * Creates and executes a periodic action that becomes enabled first * after the given initial delay, and subsequently with the * given delay between the termination of one execution and the * commencement of the next. If any execution of the task * encounters an exception, subsequent executions are suppressed. * Otherwise, the task will only terminate via cancellation or * termination of the executor. 创建执行一个周期性间隔为period的任务,在初始化延时initialDelay后,执行第一个周期性任务, 后续任务执行的延迟时间为前一个任务执行结束和下一个任务开始的时间间隔。我们假设initialDelay为0,delay为2, 每个任务执行需要3秒,第一个任务执行后为3秒,下一个任务执行的时间为5秒(3+2)。有时候 0---3--5---8--10----14--16-----... t0-----t1(5)-----t2(10)------t3(16)-----... 我们假设t2执行的时间为4秒,那t3执行的时间点为16。 如果任务有一个任务执行异常,则后续的任务将会被取消。另外任务执行被结束通过取消或执行器执行结束。 * * @param command the task to execute * @param initialDelay the time to delay first execution * @param delay the delay between the termination of one * execution and the commencement of the next * @param unit the time unit of the initialDelay and delay parameters * @return a ScheduledFuture representing pending completion of * the task, and whose <tt>get()</tt> method will throw an * exception upon cancellation * @throws RejectedExecutionException if the task cannot be * scheduled for execution * @throws NullPointerException if command is null * @throws IllegalArgumentException if delay less than or equal to zero */ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); }
//ScheduledFuture,继承了Delayed和Future接口
/** * A delayed result-bearing action that can be cancelled. * Usually a scheduled future is the result of scheduling * a task with a {@link ScheduledExecutorService}. * * @since 1.5 * @author Doug Lea * @param <V> The result type returned by this Future */ public interface ScheduledFuture<V> extends Delayed, Future<V> { }
发表评论
-
Executors解析
2017-04-07 14:38 1245ThreadPoolExecutor解析一(核心线程池数量、线 ... -
ScheduledThreadPoolExecutor解析三(关闭线程池)
2017-04-06 20:52 4451ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析二(任务调度)
2017-04-06 12:56 2117ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析一(调度任务,任务队列)
2017-04-04 22:59 4987Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析四(线程池关闭)
2017-04-03 23:02 9100Executor接口的定义:http: ... -
ThreadPoolExecutor解析三(线程池执行提交任务)
2017-04-03 12:06 6081Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析二(线程工厂、工作线程,拒绝策略等)
2017-04-01 17:12 3036Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析一(核心线程池数量、线程池状态等)
2017-03-31 22:01 20514Executor接口的定义:http://donald-dra ... -
AbstractExecutorService解析
2017-03-29 08:27 1071Executor接口的定义:http: ... -
ExecutorCompletionService解析
2017-03-28 14:27 1586Executor接口的定义:http://donald-dra ... -
CompletionService接口定义
2017-03-28 12:39 1061Executor接口的定义:http://donald-dra ... -
FutureTask解析
2017-03-27 12:59 1325package java.util.concurrent; ... -
Future接口定义
2017-03-26 09:40 1193/* * Written by Doug Lea with ... -
ExecutorService接口定义
2017-03-25 22:14 1159Executor接口的定义:http://donald-dra ... -
Executor接口的定义
2017-03-24 23:24 1672package java.util.concurrent; ... -
简单测试线程池拒绝执行任务策略
2017-03-24 22:37 2025线程池多余任务的拒绝执行策略有四中,分别是直接丢弃任务Disc ... -
JAVA集合类简单综述
2017-03-23 22:51 920Queue接口定义:http://donald-draper. ... -
DelayQueue解析
2017-03-23 11:00 1733Queue接口定义:http://donald-draper. ... -
SynchronousQueue解析下-TransferQueue
2017-03-22 22:20 2133Queue接口定义:http://donald-draper. ... -
SynchronousQueue解析上-TransferStack
2017-03-21 22:08 3048Queue接口定义:http://donald-draper. ...
相关推荐
ExecutorService接口定义了对Executor的服务, ScheduledExecutorService接口定义了定时调度接口,而AbstractExecutorService抽象类则是执行框架的抽象实现。 ThreadPoolExecutor是JDK中线程池的具体实现,提供了一...
接口定义了一组方法签名,但不包含实现,这使得实现这些接口的类可以自由地提供自己的实现逻辑。 计时时钟的设计通常包括以下几个关键部分: 1. **时间管理**:计时器的核心在于准确地跟踪和表示时间。这需要对...
3. **ScheduledExecutorService接口**:除了异步执行外,Spring还提供了定时执行任务的能力。ScheduledExecutorService接口提供了延迟执行和周期性执行任务的方法。 二、Spring定时器(TaskScheduler) 1. **Task...
ScheduledExecutorService接口扩展了ExecutorService接口,并增加了定时和周期性执行任务的能力。这表示该接口可以安排在未来某一时刻执行任务,或周期性重复执行。 ExecutorService是Executor的一个子接口,增加了...
本实例将深入探讨如何使用`ScheduledExecutorService`接口来实现这一功能。`ScheduledExecutorService`是Java并发包`java.util.concurrent`中的一个接口,它提供了延迟执行和周期性执行任务的能力。 首先,我们需要...
该接口定义了三类定时方法: 1. schedule方法 schedule方法用于在给定的时间对任务进行一次性调度。该方法有两个重载版本: ```java public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit...
对于需要定时执行的任务,Java并发库提供了`ScheduledExecutorService`接口。这个接口允许你安排任务在未来某个时间点或周期性地执行。例如,`ScheduledExecutorService`可以用于实现定时任务,如统计、优化、数据...
2. 服务接口定义:定义了服务启动、停止等操作的接口,供看门狗和服务实现类交互。 3. 服务实现类:实现了服务接口,包含了服务的实际启动和停止逻辑。 4. 配置文件:可能包含了看门狗的配置信息,如检查间隔、日志...
而`java.util.TimerTask`是实现了Runnable接口的抽象类,它是所有定时任务的基础,你需要继承它并重写`run()`方法来定义自己的任务逻辑。 创建一个定时任务的基本步骤如下: 1. 创建`TimerTask`子类:首先,你需要...
Java提供了多种实现定时任务的方法,包括`java.util.Timer`类和`java.util.concurrent.ScheduledExecutorService`接口。下面将详细讨论这些知识点。 1. **java.util.Timer**: `Timer`类是Java标准库中的一个基础...
`java.util.concurrent.ScheduledExecutorService` 接口 `ScheduledExecutorService` 是Java并发包(`java.util.concurrent`)的一部分,提供更强大、灵活的定时任务调度功能。它支持多线程任务执行,并且可以取消...
在这个例子中,`TimerTask`是一个实现了Runnable接口的匿名内部类,它的`run()`方法定义了要定时执行的任务。`Timer`的`schedule()`方法用于设置任务的初始延迟和周期性执行的时间间隔。 然而,`Timer`类有一些局限...
`ScheduledExecutorService`是`ExecutorService`接口的一个子接口,提供了更高级的定时和延迟执行任务的能力。与`Timer`相比,`ScheduledExecutorService`是线程安全的,更适合多线程环境。它可以处理多个并发任务,...
通过实现`Runnable`或`Callable`接口并使用`ScheduledExecutorService`的`schedule`、`scheduleAtFixedRate`和`scheduleWithFixedDelay`方法来安排任务。 2. **Timer和TimerTask** 在较早的Java版本中,`java.util...
使用`Runnable`或`Callable`接口定义任务,并通过`submit()`或`execute()`方法提交到`ScheduledExecutorService`进行执行。在设计定时提醒时,需要考虑任务的触发时机、重复频率、取消和暂停机制等要素。 为了提供...
Spring通过实现ScheduledExecutorService接口的ScheduledExecutorBean,提供了ScheduledTasks的支持。我们可以通过@Scheduled注解来定义一个方法为定时任务,例如每5秒执行一次,或者在特定时间点执行。 当涉及...
在Java中,有多种方式可以实现任务调度,包括基础的`Timer`类、`ScheduledExecutorService`接口以及第三方库如Quartz和JCronTab。 首先,我们来看`Timer`类。`Timer`是最简单的任务调度实现,它基于单线程模型,...
而 `ScheduledExecutorService` 是 `ExecutorService` 的子接口,增加了定时及周期性任务的执行能力。 在提供的代码示例中,`BeeperControl` 类展示了如何使用 `ScheduledExecutorService` 来实现定时任务调度。...
线程池中的重要顶层接口和抽象类,如ExecutorService、AbstractExecutorService和ScheduledExecutorService,提供了基础的线程池服务和调度功能。这些接口和抽象类为开发者构建和定制线程池提供了灵活性,可以根据...