Verifying Periodic Run ScheduledFuture

I have a periodic task scheduled using Spring TaskScheduler.schedule(Runnable, Trigger).

Given the returnee ScheduledFuture, is there a way to check if the job is currently running?

+3
source share
2 answers

You can modify Runnable as follows:

class Runner implements Runnable{
    public volatile boolean RUNNING = false;
        public void run(){

        RUNNING = true;
        try{
            // Your code
        } finally {
            RUNNING = false;
        }
    }
}

change Muscles with booleanare atomic and do not need variability.

+2
source

After a little testing

public static boolean isRunning(ScheduledFuture future) {
    return future.getDelay(TimeUnit.MILLISECONDS) <= 0;
}

works like a charm. It seems that the task is reconfigured only after completion, so it getDelay()returns a negative value.

+7
source

Source: https://habr.com/ru/post/1788143/


All Articles