Search code examples
javaconcurrencyscheduling

Checking if periodic ScheduledFuture is running now


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

Given the returned ScheduledFuture, is there any way to check, if the task is running at current moment?


Solution

  • You can change you Runnable like this:

    class Runner implements Runnable{
        public volatile boolean RUNNING = false;
            public void run(){
    
            RUNNING = true;
            try{
                // Your code
            } finally {
                RUNNING = false;
            }
        }
    }
    

    edit Thought operations with boolean are atomic and don't need to be volatile.