Reputation: 2040
Can I know what is the best way to schedule a method run every x seconds, if the method itself takes certain time (less than x seconds) to run?
For example:
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
class Helper extends TimerTask
{
public void run()
{
System.out.println("Start");
//for example, the program takes about 100 ms to run and the exact time may vary
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("End");
}
}
public class Test
{
public static void main(String[] args)
{
Timer timer = new Timer();
TimerTask task = new Helper();
timer.schedule(task, 0, 1000);
}
}
How can I guarantee after 0s, the second time the function gets scheduled is 1s (with no delay) and not 1.1s?
Upvotes: 0
Views: 34
Reputation: 18792
To start a task x milliseconds after the previous one ended you can use ScheduledExecutorService
:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Test
{
public static void main(String[] args)
{
ScheduledExecutorService seService = Executors.newSingleThreadScheduledExecutor();
Runnable task = new Helper(seService);
seService.execute(task);
}
}
class Helper implements Runnable
{
private final ScheduledExecutorService seService;
Helper(ScheduledExecutorService seService) {
this.seService = seService;
}
@Override
public void run()
{
System.out.println("Start");
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) { e.printStackTrace(); }
seService.schedule(this, 1000, TimeUnit.MILLISECONDS); //execute again after delay
System.out.println("End");
}
}
Upvotes: 1
Reputation: 381
change your code like this:
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
class Helper extends TimerTask
{
public void run()
{
long current = System.currentTimeMillis();
System.out.println("Start");
//for example, the program takes about 100 ms to run and the exact time may vary
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("End");
long after = System.currentTimeMillis();
Timer timer = new Timer();
TimerTask task = new Helper();
timer.schedule(task, 0, 1000 - (after - current));
}
}
public class Test
{
public static void main(String[] args)
{
Timer timer = new Timer();
TimerTask task = new Helper();
timer.schedule(task, 0, 1000);
}
}
another option is using thread.sleep():
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
class Helper extends TimerTask
{
public void run()
{
while(true)
{
long current = System.currentTimeMillis();
System.out.println("Start");
//for example, the program takes about 100 ms to run and the exact time may vary
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("End");
long after = System.currentTimeMillis();
Thread.sleep(1000 - (after - current));
}
}
}
public class Test
{
public static void main(String[] args)
{
Timer timer = new Timer();
TimerTask task = new Helper();
timer.schedule(task, 0, 1000);
}
}
Upvotes: 0