本篇內容主要講解“Java調度線程池ScheduledThreadPoolExecutor不執行問題怎么解決”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Java調度線程池ScheduledThreadPoolExecutor不執行問題怎么解決”吧!
在示例程序可以看到當計數器中的計數達到5的時候就會主動拋出一個異常,拋出異常后ScheduledThreadPoolExecutor就不調度了。
public class ScheduledTask {
private static final AtomicInteger count = new AtomicInteger(0);
private static final ScheduledThreadPoolExecutor SCHEDULED_TASK = new ScheduledThreadPoolExecutor(
1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, "sc-task");
t.setDaemon(true);
return t;
}
});
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
SCHEDULED_TASK.scheduleWithFixedDelay(() -> {
System.out.println(111);
if (count.get() == 5) {
throw new IllegalArgumentException("my exception");
}
count.incrementAndGet();
}, 0, 5, TimeUnit.SECONDS);
latch.await();
}
}ScheduledThreadPoolExecutor#run
run方法內部首先判斷任務是不是周期性的任務,如果不是周期性任務通過ScheduledFutureTask.super.run();執行任務;如果狀態是運行中或shutdown,取消任務執行;如果是周期性的任務,通過ScheduledFutureTask.super.runAndReset()執行任務并且重新設置狀態,成功了就會執行setNextRunTime設置下次調度的時間,問題就是出現在ScheduledFutureTask.super.runAndReset(),這里執行任務出現了異常,導致結果為false,就不進行下次調度時間設置等
public void run() {
boolean periodic = isPeriodic();
if (!canRunInCurrentRunState(periodic))
cancel(false);
else if (!periodic)
ScheduledFutureTask.super.run();
else if (ScheduledFutureTask.super.runAndReset()) {
setNextRunTime();
reExecutePeriodic(outerTask);
}
}*FutureTask#runAndReset
在線程任務執行過程中拋出異常,然后catch到了異常,最終導致這個方法返回false,然后ScheduledThreadPoolExecutor#run就不設置下次執行時間了,代碼c.call(); 拋出異常,跳過ran = true;代碼,最終runAndReset返回false。
protected boolean runAndReset() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return false;
boolean ran = false;
int s = state;
try {
Callable<V> c = callable;
if (c != null && s == NEW) {
try {
c.call(); // don't set result
ran = true;
} catch (Throwable ex) {
setException(ex);
}
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
return ran && s == NEW;
}到此,相信大家對“Java調度線程池ScheduledThreadPoolExecutor不執行問題怎么解決”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。