JAVA线程池ScheduledExecutorService周期性地执行任务 与单个Thread周期性执行任务的异常处理...
本文記錄:
1,使用ScheduledExecutorService的?scheduleAtFixedRate 方法執(zhí)行周期性任務(wù)的過程,討論了在任務(wù)周期執(zhí)行過程中出現(xiàn)了異常,會導(dǎo)致周期任務(wù)失敗。
2,使用普通的Thread類來執(zhí)行任務(wù),在main線程中周期性創(chuàng)建線程,提交任務(wù)。然后,使用UncaughtExceptionHandler來處理異常。?
?
一,正常任務(wù)執(zhí)行
負(fù)責(zé)執(zhí)行任務(wù)的線程類如下:(一個計(jì)算階乘的任務(wù),計(jì)算5以上的階乘,就會拋出異常)
1 public class FactorialCalc implements Runnable { 2 3 private Integer number; 4 5 public FactorialCalc(Integer number) { 6 this.number = number; 7 } 8 9 public void run() { 10 11 int result = 1; 12 13 if (number == 0) { 14 System.out.println("0!=" + "1"); 15 } 16 17 if (number > 5) { 18 System.out.println("exception"); 19 throw new IllegalArgumentException(">5"); 20 } 21 22 for(int i = 1; i <= number; i++) { 23 result *= i; 24 25 } 26 System.out.println(number + "!=" + result); 27 } 28 }?
測試的Main類如下:
1 public class MainPeriod { 2 3 public static void main(String[] args) { 4 5 ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2); 6 7 FactorialCalc task1 = new FactorialCalc(6); 8 FactorialCalc task2 = new FactorialCalc(3); 9 10 ScheduledFuture<?> result = executorService.scheduleAtFixedRate(task1, 0, 1, TimeUnit.SECONDS); 11 // ScheduledFuture<?> result = executorService.scheduleAtFixedRate(task2, 0, 2, TimeUnit.SECONDS); 12 13 try { 14 TimeUnit.SECONDS.sleep(5); 15 executorService.shutdown(); 16 } catch (InterruptedException e) { 17 e.printStackTrace(); 18 } 19 } 20 }?
ScheduledFuture<?> result = executorService.scheduleAtFixedRate(task1, 0, 1, TimeUnit.SECONDS);提交一個Runnable任務(wù),延遲為0,每1秒鐘執(zhí)行一次。
?
二,線程 執(zhí)行過程中出現(xiàn)異常
當(dāng)提交 task1 時,線程在執(zhí)行過程中會拋出異常。
FactorialCalc task1 = new FactorialCalc(6);//計(jì)算6的階乘,6大于5ScheduledFuture<?> result = executorService.scheduleAtFixedRate(task1, 0, 1, TimeUnit.SECONDS);此時,如果注釋掉 //executorService.shutdown(); 則線程池不會中止,因?yàn)檫@是一個線程池,所有的異常都由線程池catch住了。但是由于線程執(zhí)行過程中拋出了異常,任務(wù)也不會周期性地執(zhí)行了。參考JDK里面的scheduleAtFixedRate注釋:
* If any execution of the task* encounters an exception, subsequent executions are suppressed.public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);?
三,關(guān)閉線程池 ,執(zhí)行 executorService.shutdown() 語句
若線程池線程 執(zhí)行任務(wù)過程中拋出了異常,但是在 主線程中 執(zhí)行了executorService.shutdown() 語句,則會正常關(guān)閉 線程池。
?
四,總結(jié)
使用ScheduledExecutorService的?scheduleAtFixedRate 方法執(zhí)行周期性任務(wù)時,如果任務(wù)一直正常執(zhí)行,則任務(wù)會按設(shè)定的執(zhí)行周期一直在運(yùn)行(前提是,主線程內(nèi)不要調(diào)用executorService.shutdown() ,比如需要 執(zhí)行 永久性的周期性任務(wù),那就不能調(diào)用 executorService.shutdown()?)。
如果任務(wù)在某次執(zhí)行過程中拋出了異常,則周期性任務(wù)會被中斷,后續(xù)也不會再生成任務(wù)了,如果主線程 也沒有 調(diào)用?executorService.shutdown() ,那線程池就不會關(guān)閉了。
?
五,使用Thread類執(zhí)行任務(wù),在Main線程中通過while循環(huán)周期性提交任務(wù),使用UncaughtExceptionHandler來處理異常
1 import java.util.Random; 2 import java.util.concurrent.TimeUnit; 3 4 public class Main { 5 6 public static void main(String[] args) { 7 8 Random rand = new Random(); 9 10 while(true){ 11 int number = rand.nextInt(10); 12 Thread t = new Thread(new FactorialCalc(number)); 13 t.setUncaughtExceptionHandler(new ExceptionHandler()); 14 t.start(); 15 try{ 16 System.out.println("sleep 4s for next task"); 17 TimeUnit.SECONDS.sleep(4); 18 }catch(InterruptedException e){ 19 20 } 21 } 22 } 23 }?在main方法中使用一個while(true)循環(huán),周期性地創(chuàng)建線程 提交任務(wù)。
第12-13行,每創(chuàng)建一個線程,調(diào)用setUncaughtExceptionHandler方法設(shè)置異常處理。關(guān)于異常處理,可參考:JAVA線程未捕獲異常處理
第15-18行,線程每隔4s提交一次任務(wù),從而實(shí)現(xiàn)任務(wù)的周期性執(zhí)行。?
?
異常處理類ExceptionHandler類實(shí)現(xiàn)了UncaughtExceptionHandler接口,然后在uncaughtException方法里面定義具體的異常處理過程即可。
import java.lang.Thread.UncaughtExceptionHandler;public class ExceptionHandler implements UncaughtExceptionHandler{@Overridepublic void uncaughtException(Thread t, Throwable e) {System.out.println("illegal exception: 計(jì)算的階乘大于5了," + e.getMessage());} }?
與線程池方式相比 ,這種方式是每個周期,都要new一個線程。而線程池則是每個周期new一個任務(wù),把任務(wù)提交給線程池即可。
原文:http://www.cnblogs.com/hapjin/p/7616068.html
轉(zhuǎn)載于:https://www.cnblogs.com/hapjin/p/7616068.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的JAVA线程池ScheduledExecutorService周期性地执行任务 与单个Thread周期性执行任务的异常处理...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 安装lnmp
- 下一篇: 2017-2018-2 20155327