Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scheduleAtFixedRate not executing after first run

I have a scheduled executor to reset a parameter to 0 and awake all active threads to continue processing. However after initial run of the thread it is not executing again.

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

There is another Executor defined in the class which executes further processes and not sure if this influences it:

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();
        }
}
like image 394
Charabon Avatar asked Sep 13 '17 13:09

Charabon


2 Answers

From the documentation for ScheduledExecutorService.scheduleAtFixedRate():

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

So something in your task is throwing an exception. I would guess the call to executor.notifyAll() which is documented to throw an IllegalMonitorStateException:

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

like image 136
Sean Bright Avatar answered Sep 29 '22 07:09

Sean Bright


Your scheduled task will most probably end up in a uncaught Exception. Taken from the JavaDoc of ScheduledExecutorService.scheduleAtFixedRate

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

Because you are provoking a uncaught exception, all further executions are cancelled.

like image 35
Herr Derb Avatar answered Sep 29 '22 06:09

Herr Derb