java實(shí)現(xiàn)周期性執(zhí)行(定時(shí)任務(wù))
最近是遇到一個(gè)設(shè)備在線離線的判定問題,設(shè)計(jì)是每個(gè)多長(zhǎng)時(shí)間(常規(guī)的定時(shí)任務(wù))檢測(cè)一次設(shè)備是否在前,當(dāng)檢測(cè)到里離線時(shí),我們不能立馬判斷為離線,而是要在重試多測(cè)幾次,只要一次成功就返回判定為在線,多次都不成功側(cè)是離線,我這里相當(dāng)了用ScheduledThreadPoolExecutor來實(shí)現(xiàn),如有不足還請(qǐng)?zhí)岢?。如下?/p>
ScheduledThreadPoolExecutor的介紹:
ScheduledThreadPoolExecutor,它可另行安排在給定的延遲后運(yùn)行命令,或者定期執(zhí)行命令。需要多個(gè)輔助線程時(shí),或者要求 ThreadPoolExecutor 具有額外的靈活性或功能時(shí),此類要優(yōu)于Timer。
ScheduledThreadPoolExecutor的使用詳解
當(dāng)程序需要用到一個(gè)定時(shí)器處理問題的時(shí)候,并且需要處理的頻率是很快的,這就需要一個(gè)穩(wěn)定的定時(shí)器來保證數(shù)據(jù)的長(zhǎng)久進(jìn)行。ScheduledThreadPoolExecutor這個(gè)類就是個(gè)很好的選擇。正常情況下,定時(shí)器我們都是用Timer和TimerTask這兩個(gè)類就能完成定時(shí)任務(wù),并且設(shè)置延長(zhǎng)時(shí)間和循環(huán)時(shí)間間隔。
ScheduledThreadPoolExecutor也能完成Timer一樣的定時(shí)任務(wù),并且時(shí)間間隔更加準(zhǔn)確。
誤差說明:
我在后臺(tái)程序看看一下Timer執(zhí)行程序是有可能延遲1、2毫秒,如果是1秒執(zhí)行一次的任務(wù),1分鐘有可能延遲60毫秒,一小時(shí)延遲3600毫秒,相當(dāng)于3秒,實(shí)際用戶看不出什么區(qū)別。 但是,如果我的程序需要每40毫秒就執(zhí)行一次任務(wù),如果還是有1、2毫秒的誤差,1秒鐘就有25毫秒的誤差,大概40秒就有1秒的誤差,十幾分鐘就有十幾秒的誤差,這對(duì)UI顯示來說是延遲非常嚴(yán)重的了。 而我用ScheduledThreadPoolExecutor來做40毫秒的間隔任務(wù),一般十幾分鐘才有1秒多的誤差,這個(gè)還是能接受的。 這也是我用ScheduledThreadPoolExecutor這個(gè)類的原因。
使用Timer和TimerTask存在一些缺陷:
1.Timer只創(chuàng)建了一個(gè)線程。當(dāng)你的任務(wù)執(zhí)行的時(shí)間超過設(shè)置的延時(shí)時(shí)間將會(huì)產(chǎn)生一些問題。
2.Timer創(chuàng)建的線程沒有處理異常,因此一旦拋出非受檢異常,該線程會(huì)立即終止。
JDK 5.0以后推薦使用ScheduledThreadPoolExecutor。該類屬于Executor Framework,它除了能處理異常外,還可以創(chuàng)建多個(gè)線程解決上面的問題
Timer和TimerTask的使用 :
這里就不做過多的描述了,重點(diǎn)在ScheduledThreadPoolExecutor。
Timer timer = new Timer(); ? ? ? timer.schedule(new TimerTask() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void run() { ? ? ? ? ? ? ? ? log.e("time:"); ? ? ? ? ? ? ? } ? ? ? ? }, 2000, 40); //2000表示第一次執(zhí)行任務(wù)延遲時(shí)間,40表示以后每隔多長(zhǎng)時(shí)間執(zhí)行一次run里面的任務(wù)
ScheduledThreadPoolExecutor的使用:
import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; ? /** ?* 設(shè)備在線延時(shí)檢查檢查 ?*/ @Slf4j public class DelayedCheckDeviceSchedule { ? ? public static final Integer CONNECT_TIME_OUT = 10000; ? //引用的業(yè)務(wù)層 ? private ITblBaseService baseService = SpringContextHolder.getBean(ITblBaseService .class); ? ? private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = null; ? ? /** ? ?* 需要延時(shí)檢查的設(shè)備狀態(tài)的設(shè)備id集合 ? ?*/ ? public static Set<String> deviceSet = new HashSet<>(); ? ? /** ? ?* 當(dāng)前執(zhí)行點(diǎn) ? ?*/ ? private AtomicInteger currentAtomicInteger = new AtomicInteger(1); ? ? ? /** ? ?* 初始化任務(wù) ? ?* @param delay 延遲幾秒執(zhí)行 ? ?* @param checkCount 需要檢測(cè)的次數(shù) ? ?* @param deviceId 設(shè)備Id ? ?* @param deviceType 設(shè)備類型 ? ?* @return ? ?*/ ? public boolean init(long delay, int checkCount, String deviceId, String deviceType) { ? ? log.info("第一次初始化時(shí)間"+deviceId+":"+System.currentTimeMillis()); ? ? if (deviceSet.add(deviceId) && deviceConnectModel!=null) { ? ? ? this.scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(4); ? ? ? this.scheduledThreadPoolExecutor.schedule(new Runnable() { ? ? ? ? @Override ? ? ? ? public void run() { ? ? ? ? ? executor(delay, checkCount, deviceId, deviceType); ? ? ? ? } ? ? ? }, delay, TimeUnit.SECONDS); ? ? ? return true; ? ? } ? ? return false; ? } ? ? /** ? ?* 執(zhí)行體 ? ?*/ ? private void executor(long delay, int checkCount, String deviceId, String deviceType) { ? ? log.info("第"+currentAtomicInteger.get()+"執(zhí)行時(shí)間"+deviceId+":"+System.currentTimeMillis()); ? ? if (deviceSet.contains(deviceId) && currentAtomicInteger.get() < (checkCount+1)) { ? ? ? //執(zhí)行邏輯 ? ? ? ? ?? ? ? ? ?//當(dāng)滿足條件時(shí),停止任務(wù) ? ? ? ? if(currentAtomicInteger.get()==checkCount){? ? ? ? ? ? //需要處理的邏輯 ? ? ? ? ? ? //停止任務(wù) ? ? ? ? ? this.scheduledThreadPoolExecutor.shutdownNow(); ? ? ? ? ? deviceSet.remove(deviceId); ? ? ? ? }else { ? ? ? ? ? //下一次執(zhí)行 ? ? ? ? ? currentAtomicInteger.getAndIncrement(); ? ? ? ? ? this.scheduledThreadPoolExecutor.schedule(new Runnable() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void run() { ? ? ? ? ? ? ? executor(delay, checkCount, deviceId, deviceType); ? ? ? ? ? ? } ? ? ? ? ? }, delay, TimeUnit.SECONDS); ? ? ? ? ? ? ?} ? ? } else { ? ? ? this.scheduledThreadPoolExecutor.shutdownNow(); ? ? ? this.scheduledThreadPoolExecutor = null; ? ? } ? } ? ? /** ? ?* 停止檢測(cè)任務(wù) ? ?* @param deviceId ? ?* @return ? ?*/ ? public static boolean stop(String deviceId) { ? ? return deviceSet.remove(deviceId); ? } ? }
在需要周期性檢查的時(shí)候引入:
DelayedCheckDeviceSchedule delayedCheckDeviceSchedule = new DelayedCheckDeviceSchedule(); delayedCheckDeviceSchedule.init(10, 3, panModel.getDeviceId(), "pan");
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot項(xiàng)目@RestController使用重定向redirect方式
這篇文章主要介紹了Spring Boot項(xiàng)目@RestController使用重定向redirect方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Spring Data Jpa Mysql使用utf8mb4編碼的示例代碼
這篇文章主要介紹了Spring Data Jpa Mysql使用utf8mb4編碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11Java使用Jdbc連接Oracle執(zhí)行簡(jiǎn)單查詢操作示例
這篇文章主要介紹了Java使用Jdbc連接Oracle執(zhí)行簡(jiǎn)單查詢操作,結(jié)合實(shí)例形式詳細(xì)分析了java基于jdbc實(shí)現(xiàn)Oracle數(shù)據(jù)庫的連接與查詢相關(guān)操作技巧,需要的朋友可以參考下2019-09-09Value注解支持對(duì)象類型ConfigurationProperties功能
這篇文章主要為大家介紹了Value注解支持對(duì)象類型ConfigurationProperties功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Maven項(xiàng)目web多圖片上傳及格式驗(yàn)證的實(shí)現(xiàn)
本文主要介紹了Maven項(xiàng)目web多圖片上傳及格式驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10java多線程編程之使用runnable接口創(chuàng)建線程
實(shí)現(xiàn)Runnable接口的類必須使用Thread類的實(shí)例才能創(chuàng)建線程,通過Runnable接口創(chuàng)建線程分為以下兩步2014-01-01java開發(fā)之spring webflow實(shí)現(xiàn)上傳單個(gè)文件及多個(gè)文件功能實(shí)例
這篇文章主要介紹了java開發(fā)之spring webflow實(shí)現(xiàn)上傳單個(gè)文件及多個(gè)文件功能,結(jié)合具體實(shí)例形式分析了spring webflow文件上傳具體操作技巧,需要的朋友可以參考下2017-11-11