A typical usage of java scheduled executor looks like this

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
 executor.scheduleWithFixedDelay(() -> System.out.println("Hello"), 0, 1, TimeUnit.SECONDS);

However, we shall be careful of the pitfall marked below. When there exists potential failing codes, it’s better to try catch the block so that the job can keep running without unexpected termination.

对于Java的定时任务类ScheduledExecutorService,如果提交的周期任务可能出错,建议加上try catch以防止后续任务不再执行。

/**
 * 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.
 *
 * @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 {@code get()} 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);

Java scheduled executor的更多相关文章

  1. Java的Executor框架和线程池实现原理

    Java的Executor框架 1,Executor接口 public interface Executor { void execute(Runnable command); } Executor接 ...

  2. java中Executor、ExecutorService、ThreadPoolExecutor介绍(转)

    1.Excutor 源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /**     * Executes th ...

  3. java使用Executor(执行器)管理线程

    一.一个实现了Runnable接口的类 class MyThread implements Runnable{ private static int num = 0; @Override public ...

  4. JAVA的Executor框架

    Executor框架分离了任务的创建和执行.JAVA SE5的java.util.concurrent包中的执行器(Executor)管理Thread对象,从而简化了并发编程.Executor引入了一 ...

  5. java中Executor、ExecutorService、ThreadPoolExecutor介绍

    源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /**      * Executes the given c ...

  6. JAVA并发-Executor

    结构 类继承图: 上面的各个接口/类的关系和作用: Executor 执行器接口,也是最顶层的抽象核心接口, 分离了任务和任务的执行. ExecutorService 在Executor的基础上提供了 ...

  7. Java的Executor框架和线程池实现原理(转)

    ExecutorService接口继承自Executor接口,定义了终止.提交,执行任务.跟踪任务返回结果等方法 1,execute(Runnable command):履行Ruannable类型的任 ...

  8. 在 Java 中 Executor 和 Executors 的区别?

    Executors 工具类的不同方法按照我们的需求创建了不同的线程池,来满足业务 的需求. Executor 接口对象能执行我们的线程任务. ExecutorService 接口继承了 Executo ...

  9. (转)java中Executor、ExecutorService、ThreadPoolExecutor介绍

    转自: http://blog.csdn.net/linghu_java/article/details/17123057 ScheduledThreadPoolExecutor介绍: http:// ...

随机推荐

  1. Android Studio开发遇到程序崩溃问题

    在用Android Studio开发过程中,经常遇到程序本身没有错误,但运行起来却总是挂掉,具体有如下几个解决方案: 1.将运行在真机上的app卸载,重新运行安装 2.在Build选项中有一个clea ...

  2. download 下载文件 IE兼容性处理

    根据CANIUSE(http://caniuse.com/#search=download)download兼容性如下图所示: 如上图所示,IE浏览器是不支持的. 1.测试代码: <!docty ...

  3. java设计模式(1)

    设计模式定义 设计模式原则 设计模式分类 常用设计模式 (一)设计模式定义 设计模式是针对软件设计中普遍存在的各种问题,所提出的解决方案. 换句话说,设计模式是一套被反复使用,多数人知晓的.经过分类的 ...

  4. 部署在服务器中的WebService

    1.继上篇之后,我把我的WebService部署在了比较小的Tomcat中,首先要把Jax-ws.jar架包放在lib里面 2.在我的WEB-INF里面增加了一个xml文件:sun-jaxws.xml ...

  5. pyqt样式表语法笔记(中)--原创

    pyqt样式表语法笔记(中) pyqt QSS python 样式表 一.弹窗 在日常的各种桌面软件的使用中,我们都会碰到弹窗.例如注册,登录的时候,会有相应的信息弹窗,这里就以信息收集弹窗为例进行弹 ...

  6. 第5章Zabbix自动化监控

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; t ...

  7. 老李分享知识:性能测试之TPS和吞吐率

    老李分享知识:性能测试之TPS和吞吐率        当增大系统的压力(或添加并发用户数)时,吞吐率和TPS的改变曲线呈大体一致,则系统基本稳定. 若压力增大时,吞吐率的曲线添加到一定程度后出现改变缓 ...

  8. Spring基础学习(一)—初识Spring

    一.Spring的使用 1.导入jar包 2.编写实体类 Person.java public class Person{ private String name; public void say() ...

  9. RabbitMQ-从基础到实战(6)— 与Spring集成

    0.目录 RabbitMQ-从基础到实战(1)- Hello RabbitMQ RabbitMQ-从基础到实战(2)- 防止消息丢失 RabbitMQ-从基础到实战(3)- 消息的交换(上) Rabb ...

  10. 在jsp中用一数组存储了数据库表中某一字段的值,然后在页面中输出其中的值。

    List<String> list = new ArrayList<String>();  String sql = "select userName from us ...