欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)周期性執(zhí)行(定時任務(wù))

 更新時間:2022年09月02日 16:22:40   作者:你是我的小丫小太陽  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)周期性執(zhí)行定時任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近是遇到一個設(shè)備在線離線的判定問題,設(shè)計是每個多長時間(常規(guī)的定時任務(wù))檢測一次設(shè)備是否在前,當(dāng)檢測到里離線時,我們不能立馬判斷為離線,而是要在重試多測幾次,只要一次成功就返回判定為在線,多次都不成功側(cè)是離線,我這里相當(dāng)了用ScheduledThreadPoolExecutor來實(shí)現(xiàn),如有不足還請?zhí)岢觥H缦拢?/p>

ScheduledThreadPoolExecutor的介紹:

ScheduledThreadPoolExecutor,它可另行安排在給定的延遲后運(yùn)行命令,或者定期執(zhí)行命令。需要多個輔助線程時,或者要求 ThreadPoolExecutor 具有額外的靈活性或功能時,此類要優(yōu)于Timer。

ScheduledThreadPoolExecutor的使用詳解

當(dāng)程序需要用到一個定時器處理問題的時候,并且需要處理的頻率是很快的,這就需要一個穩(wěn)定的定時器來保證數(shù)據(jù)的長久進(jìn)行。ScheduledThreadPoolExecutor這個類就是個很好的選擇。正常情況下,定時器我們都是用Timer和TimerTask這兩個類就能完成定時任務(wù),并且設(shè)置延長時間和循環(huán)時間間隔。
ScheduledThreadPoolExecutor也能完成Timer一樣的定時任務(wù),并且時間間隔更加準(zhǔn)確。

誤差說明:

我在后臺程序看看一下Timer執(zhí)行程序是有可能延遲1、2毫秒,如果是1秒執(zhí)行一次的任務(wù),1分鐘有可能延遲60毫秒,一小時延遲3600毫秒,相當(dāng)于3秒,實(shí)際用戶看不出什么區(qū)別。 但是,如果我的程序需要每40毫秒就執(zhí)行一次任務(wù),如果還是有1、2毫秒的誤差,1秒鐘就有25毫秒的誤差,大概40秒就有1秒的誤差,十幾分鐘就有十幾秒的誤差,這對UI顯示來說是延遲非常嚴(yán)重的了。 而我用ScheduledThreadPoolExecutor來做40毫秒的間隔任務(wù),一般十幾分鐘才有1秒多的誤差,這個還是能接受的。 這也是我用ScheduledThreadPoolExecutor這個類的原因。

使用Timer和TimerTask存在一些缺陷:

1.Timer只創(chuàng)建了一個線程。當(dāng)你的任務(wù)執(zhí)行的時間超過設(shè)置的延時時間將會產(chǎn)生一些問題。
2.Timer創(chuàng)建的線程沒有處理異常,因此一旦拋出非受檢異常,該線程會立即終止。
JDK 5.0以后推薦使用ScheduledThreadPoolExecutor。該類屬于Executor Framework,它除了能處理異常外,還可以創(chuàng)建多個線程解決上面的問題

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ù)延遲時間,40表示以后每隔多長時間執(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è)備在線延時檢查檢查
?*/
@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è)備狀態(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 需要檢測的次數(shù)
? ?* @param deviceId 設(shè)備Id
? ?* @param deviceType 設(shè)備類型
? ?* @return
? ?*/
? public boolean init(long delay, int checkCount, String deviceId, String deviceType) {
? ? log.info("第一次初始化時間"+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í)行時間"+deviceId+":"+System.currentTimeMillis());
? ? if (deviceSet.contains(deviceId) && currentAtomicInteger.get() < (checkCount+1)) {
? ? ? //執(zhí)行邏輯
? ? ? ? ??
? ? ? ?//當(dāng)滿足條件時,停止任務(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;
? ? }
? }
?
? /**
? ?* 停止檢測任務(wù)
? ?* @param deviceId
? ?* @return
? ?*/
? public static boolean stop(String deviceId) {
? ? return deviceSet.remove(deviceId);
? }
?
}

在需要周期性檢查的時候引入:

DelayedCheckDeviceSchedule delayedCheckDeviceSchedule = new DelayedCheckDeviceSchedule();
delayedCheckDeviceSchedule.init(10, 3, panModel.getDeviceId(), "pan");

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論