scheduleAtFixedRate 在第一个 运行 之后不执行

scheduleAtFixedRate not executing after first run

我有一个预定的执行程序将参数重置为 0 并唤醒所有活动线程以继续处理。然而,在线程初始 运行 之后,它不再执行。

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);

class 中定义了另一个 Executor,它执行进一步的进程,不确定这是否会影响它:

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();
        }
}

来自the documentation for ScheduledExecutorService.scheduleAtFixedRate()

If any execution of the task encounters an exception, subsequent executions are suppressed.

所以您的任务中出现了异常。我猜想调用 executor.notifyAll() 其中 is documented 抛出一个 IllegalMonitorStateException:

if the current thread is not the owner of this object's monitor.

您的计划任务很可能会以未捕获的方式结束 Exception。取自 ScheduledExecutorService.scheduleAtFixedRate

JavaDoc

If any execution of the task encounters an exception, subsequent executions are suppressed.

因为你引发了一个未捕获的异常,所有进一步的执行都被取消。