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"); }
};
//间隔10秒种
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
//间隔一小时 取消
scheduler.schedule(new Runnable() {
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
分享到:
相关推荐
定时器小例子,这是一个经较好定时器应用
这是一个jar,很秀。自定义的一个Java定时器工具类。主要是结合Spring Boot一起使用,并在Spring Boot启动的时候一起启动运行。
主要介绍了线程并发ScheduledExecutorService类,设置 ScheduledExecutorService ,2秒后,在 1 分钟内每 10 秒钟蜂鸣一次
ScheduledExecutorService 计时器任务处理,用于超时处理。
主要介绍了ScheduledExecutorService任务定时代码示例,具有一定借鉴价值,需要的朋友可以参考下
您可以在工作线程上运行任务(Runnable或Callable),以指定执行计划(如ScheduledExecutorService),并指定执行时间。 介面 ScheduledFuture<?> schedule( Runnable command, int executeTime, long delay,...
下面小编就为大家带来一篇基于ScheduledExecutorService的两种方法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Springmvc java注解设置定时任务实例,利用 ScheduledExecutorService 接口实现
springMVC配置、注解、列子
Java通用任务执行程序,基于java.util.concurrent.ScheduledExecutorService。 Java通用的任务执行器,基于java.util.concurrent.ScheduledExecutorService工具类实现。 Maven: <groupId>...
7)一个程序执行1秒之后自动退出 13 8)线程的状态 14 9)线程阻塞的条件(重要) 14 10) Interrupt()注意事项 16 三. 线程之间协作 17 1)演示简单的消费者和生产者的例子: 17 2)管道的读写流处理方式 19 3)重要的演示...
这里的执行间隔为一天,即每天执行一次任务。 在NFDFlightDataTimerTask类中,实现了run方法,用于执行定时任务。在这里可以编写要执行的内容,例如写日志、发送邮件等。 在web.xml文件中,配置了...
ScheduledExecutorService 的测试实用程序实现 允许测试如下代码: class Foo { private int count = 0; public Foo(ScheduledExecutorService service, Bar bar) { service.scheduleAtFixedRate(new Runnable...
1.使用线程的经验:设置名称、响应中断、使用ThreadLocal 2.Executor:ExecutorService和Future 3.阻塞队列:put和take、offer和poll、drainTo 4.线程间通信:lock、condition、wait、notify、notifyAll 5.Lock-free...
1.普通thread实现 2.TimerTask实现 3.ScheduledExecutorService实现
没有通过ScheduledExecutorService或Timer定期执行某个任务实现,而是简单的通过handler发送消息去完成一次滚动,在完成一次滚动后发送另外一个delay的滚动消息,如此循环实现。自动滚动部分核心代
1、使用线程的经验:设置名称、响应中断、使用ThreadLocal 2、Executor :ExecutorService和Future ☆ ☆ ☆ 3、阻塞队列 : put和take、offer和poll、drainTo 4、线程间的协调手段:lock、condition、wait、notify、...
1、使用线程的经验:设置名称、响应中断、使用ThreadLocal 2、Executor :ExecutorService和Future ☆☆☆ 3、阻塞队列: put和take、offer和poll、drainTo 4、线程间的协调手段:lock、condition、wait、notify、...
1、使用线程的经验:设置名称、响应中断、使用ThreadLocal 2、Executor :ExecutorService和Future ☆☆☆ 3、阻塞队列: put和take、offer和poll、drainTo 4、线程间的协调手段:lock、condition、wait、notify、...