簡體   English   中英

java ScheduledExecutorService 任務在多次執行后停止

[英]java ScheduledExecutorService task stops after several executions

我有一個問題,我試圖在 ScheduledExecutorService 中執行一個任務,我正在使用以下命令執行任務:

updateTagDataHandle = scheduler.scheduleWithFixedDelay(updateTagDataRunnable, 500, 500, TimeUnit.MILLISECONDS);

並在幾次成功運行后停止。 任務本身需要幾秒鍾我用 println 檢查它沒有錯誤地結束了任務,並且我打印了任何異常並且最后沒有看到異常。 我需要它繼續運行無限次。

任何幫助,將不勝感激

編輯

我用於初始化任務調度程序的代碼是:

scheduler = Executors.newScheduledThreadPool(1);

所以 corePoolSize = 1 所以只有一個線程活着並且任務在線程上共享它。 但是將 threadPool 設置為多個並沒有幫助,而且似乎仍然只有一個線程處於活動狀態。

同樣的問題: Executors Factory method newScheduledThreadPool always returns the same Thread pool

和這里:

為什么 ScheduledExecutorService 不根據需要產生線程?

任何幫助,將不勝感激

編輯:

沒有找到解決方案,所以使用了調度程序自定義線程創建:

scheduler = Executors.newScheduledThreadPool(7, new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) {

            return new Thread(r);
        }
    });

嘗試將初始延遲更改為 1000 毫秒。
我在我的 android 應用程序中嘗試了相同的代碼,這解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM