2017-09-13 699 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

采取了任务的任一执行遇到异常,随后 执行被抑制。

因为您正在挑起未捕获的异常,所有进一步的执行都将被取消。

相关问题