2017-09-13 66 views
0

我有一個預定的執行程序將參數重置爲0並喚醒所有活動線程以繼續處理。但是,在線程初始運行後,它不會再次執行。scheduleAtFixedRate在第一次運行後沒有執行

ScheduledExecutorService exec = Executors.newScheduledThreadPool(4); 
exec.scheduleAtFixedRate(new Runnable() { 
     @Override 
     public void run() { 
      logger.info("Setting hourly limit record count back to 0 to continue processing"); 
      lines = 0; 
      executor.notifyAll(); 
      Thread.currentThread().interrupt(); 
      return; 
     } 
    }, 0, 1, TimeUnit.MINUTES); 

存在,其中,如果這會影響它執行進一步的過程,而不是確定的類定義的另一個執行人:

ExecutorService executor = Executors.newCachedThreadPool(); 
for (String processList : processFiles) { 

     String appName = processList.substring(0,processList.indexOf("-")); 
     String scope = processList.substring(processList.lastIndexOf("-") + 1); 

     logger.info("Starting execution of thread for app " + appName + " under scope: " + scope); 
     try { 
      File processedFile = new File(ConfigurationReader.processedDirectory + appName + "-" + scope + ".csv"); 
      processedFile.createNewFile(); 
      executor.execute(new APIInitialisation(appName,processedFile.length(),scope)); 
     } catch (InterruptedException | IOException e) { 
      e.printStackTrace(); 
     } 
} 
+1

中斷執行程序線程有什麼意義? –

回答

2

the documentation for ScheduledExecutorService.scheduleAtFixedRate()

如果任務遇到的任何執行一個例外,隨後的執行被壓制。

因此,在你的任務中的東西是拋出一個異常。我猜調用executor.notifyAll()is documented拋出一個IllegalMonitorStateException

如果當前線程不是這個對象監視器的所有者。

+0

完美謝謝! – Charabon

+0

如果在notifyAll操作周圍添加if語句,以確保當前活動線程愚蠢! – Charabon

0

您的計劃任務很可能會以未捕獲的Exception結束。從JavaDocScheduledExecutorService.scheduleAtFixedRate

採取了任務的任一執行遇到異常,隨後 執行被抑制。

因爲您正在挑起未捕獲的異常,所有進一步的執行都將被取消。

相關問題