3

I have a Runnable class that needs to run after a set interval since it last ran. Example: ProcessThread runs every 2 mins after it finishes. So if I start ProcessThread at 1:00, and it takes 5 mins (finishing at 1:05), the next time it should run would be at 1:07. If that one takes 3 mins to run, (finishing at 1:10), the next one starts at 1:12) and so on..

I can't set it with a Fixed Rate of 2 minutes, because then a second Thread would be fired and the first one hasn't finished yet.

So, here is my current code, but the way I have it, it keeps creating threads and never finishes them... so eventually my memory grows and grows:

Main:

public class MyMain extends Thread{
   public static void main(String[] args)  {        

    ExecuteThread execute = new ExecuteThread();
    execute.start();
   }
}

ExecuteThread (I took out the try-catch):

public void run() {

      MyProcessThread myProcess = new MyProcessThread();
      ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

      myProcess.start();
      while(myProcess.isAlive()){
         sleep(10000);
      }

      scheduler.schedule(this,myProcess.getDelaySeconds(),TimeUnit.SECONDS);

}

Before I had the scheduler run inside the MyProcessThread, but it resulted in the same. This looks to be the correct direction but something is still wrong.

2 Answers 2

4

You can do

scheduledExecutorService = Executors.newScheduledThreadPool(1);
scheduledExecutorService.scheduleWithFixedDelay(command, 0, 2, TimeUnit.MINUTES);

That will create a an executor that will run command 2 minutes after the previous command run finishes. Check the documentation here.

Here's a snippet of it.

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor.

Take into account the last sentence about if there's any exception from the command!

Sign up to request clarification or add additional context in comments.

4 Comments

I thought scheduledWithFixedDelay was to run 'command' every 2 minutes even if the first one hasn't finished.
I'm sure it works as I mentioned, as the example is a copy paste from one of the apps I've built :).
You can use "newSingleThreadScheduledExecutor()" as well as "newScheduledThreadPool(1)" both will NOT execute the command a second time if the first execution isn't fish after two minutes. But the second execution will be started immediately after the first has finished.
The documentation states very clearly "If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute". I suppose that this is exactly what we all want.
1

You can schedule next execution in finally in MyProcessThread.

1 Comment

I had this before, but someone the parent thread stays alive and never dies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.