如果方法本身需要一定的时间(少于x秒)运行,我是否知道安排每隔x秒运行一次方法的最佳方法是什么?
例如:
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);
}
}
如何保证在0s之后第二次调度函数是1s(没有延迟)而不是1.1s?
答案 0 :(得分:1)
要在上一个任务结束后的毫秒数开始任务,可以使用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");
}
}
答案 1 :(得分:0)
像这样更改代码:
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);
}
}
另一个选择是使用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);
}
}