原文:http://www.andyqian.com/2018/03/07/java/javaSmallDetail/

前言

今天我们一起来做个简单有趣的实验。熟悉Java的童鞋,对ScheduledExecutorService类应该不陌生。不记得的童鞋,先回忆下。

实验一

我们先看下下面这段简单的代码。如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ExecutoryServiceTest {

        private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);

        public static void main(String[] args){
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
int[] array = new int[1];
System.out.println("<hello world>");
System.out.println(array[1]);
}},0,2, TimeUnit.SECONDS);
}
}

够简单了吧。意思我就不再阐述了。看完别急,我们先回答下面这个问题:

问题一:

请问:上面一共打印了多少个hello world。

实验二

看到此处的童鞋,请在评论区给出你第一个实验的答案。紧接着,我们继续看第二个实验。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ExecutoryServiceTest {

        private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);

        public static void main(String[] args){
executorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
int[] array = new int[1];
System.out.println("<hello world>");
System.out.println(array[1]);
}catch(Exception ex){
ex.printStackTrace();
}
}},0,2, TimeUnit.SECONDS);
}
}

问题二:

请问: 实验二中一共打印了多少个<hello world>

请在评论区中给出你的答案。

分析

经过上述两个实验后,我们会发现两者的答案并不相同。这是为什么呢?因为在:run()方法中,发生异常后,中断了后续的执行。这是为什么呢?

其实呀,早在:scheduleAtFixedRate()JDK源码中就有这么一段描述:

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.

其意思就是告诉我们:如果任何执行任务遇到异常,其后续的操作会被压制。

同样的,在scheduleWithFixedDelay()方法中也有同样的描述。

一点点建议

  1. 在使用scheduleAtFixedRate()scheduleWithFixedDelay()时,run()方法均要在使用try{}catch处理。避免出现定时任务执行若干次后不执行的”怪现象”。

  2. 我们平时在写系统时,无论是使用JDK自带函数,还是对接外部服务。使用时,一定要了解其使用方法。对入参,结果等都充分理解。(不瞒你说,我就出现过很多次没理解充分。导致Bug产生)。

  3. 强烈建议大家都在本机上运行下上面这实验的代码。这样有利于加深影响。

ScheduledExecutorService run方法要加入try catch的更多相关文章

  1. 0040 Java学习笔记-多线程-线程run()方法中的异常

    run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...

  2. java synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较

    synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制.synchronized关键字用于保护共享数据. synchronized实现同步的机制 ...

  3. spring-boot-2.0.3启动源码篇四 - run方法(三)之createApplicationContext

    前言 此系列是针对springboot的启动,旨在于和大家一起来看看springboot启动的过程中到底做了一些什么事.如果大家对springboot的源码有所研究,可以挑些自己感兴趣或者对自己有帮助 ...

  4. 认识多线程中start和run方法的区别?

    一.认识多线程中的 start() 和 run() 1.start(): 先来看看Java API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并 ...

  5. 多线程 start 和 run 方法到底有什么区别?

    昨天栈长介绍了<Java多线程可以分组,还能这样玩!>线程分组的妙用.今天,栈长会详细介绍 Java 中的多线程 start() 和 run() 两个方法,Java 老司机请跳过,新手或者 ...

  6. spring-boot-2.0.3启动源码篇三 - run方法(二)之prepareEnvironment

    前言 此系列是针对springboot的启动,旨在于和大家一起来看看springboot启动的过程中到底做了一些什么事.如果大家对springboot的源码有所研究,可以挑些自己感兴趣或者对自己有帮助 ...

  7. spring-boot-2.0.3启动源码篇二 - run方法(一)之SpringApplicationRunListener

    前言 Springboot启动源码系列还只写了一篇,已经过去一周,又到了每周一更的时间了(是不是很熟悉?),大家有没有很期待了?我会尽量保证启动源码系列每周一更,争取不让大家每周的期望落空.一周之中可 ...

  8. spring-boot-2.0.3启动源码篇五 - run方法(四)之prepareContext

    前言 此系列是针对springboot的启动,旨在于和大家一起来看看springboot启动的过程中到底做了一些什么事.如果大家对springboot的源码有所研究,可以挑些自己感兴趣或者对自己有帮助 ...

  9. Hystrix的一个坑,queue中的run方法没有被执行?

    今天学的时候随手测了一下Hystrix的queue的异步执行,发现执行queue之后,还没有打印run方法中的内容,程序就结束了: import com.netflix.hystrix.Hystrix ...

随机推荐

  1. C json实战引擎 一 , 实现解析部分

    引言 以前可能是去年的去年,写了一个 c json 解析引擎用于一个统计实验数据项目开发中. 基本上能用. 去年在网上 看见了好多开源的c json引擎 .对其中一个比较标准的 cJSON 引擎 深入 ...

  2. 查看mysql的版本和端口号

    查看版本:select version(); 查看端口号:show global variables like 'port';

  3. git 查看父分支

    // 显示本地分支和服务器分支的映射关系 git branch -vv // 切换分支(和创建分支就差一个-b参数) git checkout {{branch_name}} // 创建新分支,新分支 ...

  4. mysql5.7 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

    mysql5.7初次登录使用提示 ERROR 1820 (HY000): You must reset your password using ALTER USER statement before ...

  5. Longest Substring Without Repeating Characters——经典题

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. apche服务器在Window和Linux下常用命令

    1.Window 1.1 启动.重启.停止——方式一(httpd) httpd.exe [-D name] [-d directory] [-f file] [-C "directive&q ...

  7. buntu 16.04上安装和配置Samba服务器

    https://www.linuxidc.com/Linux/2017-11/148194.htm

  8. 启动Tomcat报错 “A child container failed during start”

    严重: A child container failed during startjava.util.concurrent.ExecutionException: org.apache.catalin ...

  9. [jquery] 遍历select的option,然后设置一项为选中

    <script> var v={$menu.pid}; $("#pid option").each(function(){ if($(this).val()==v){ ...

  10. 洛谷P4609 [FJOI2016]建筑师(第一类斯特林数+组合数)

    题面 洛谷 题解 (图片来源于网络,侵删) 以最高的柱子\(n\)为分界线,我们将左边的一个柱子和它右边的省略号看作一个圆排列,右边的一个柱子和它左边的省略号看作一个圆排列,于是,除了中间的最高的柱子 ...