在一段时间内运行代码的更好方法

mho*_*r38 8 java timertask

我需要在预定义的时间内运行一些代码,当时间到了需要停止时.目前我正在使用TimerTask来允许代码执行一段时间,但这会导致代码创建无穷无尽的线程,而这只是效率不高.还有更好的选择吗?

当前代码;

// Calculate the new lines to draw 
            Timer timer3 = new Timer();
            timer3.schedule(new TimerTask(){
                public void run(){
                    ArrayList<String> Coords = new ArrayList<String>();
                    int x = Float.valueOf(lastFour[0]).intValue();
                    int y = Float.valueOf(lastFour[1]).intValue();
                    int x1 = Float.valueOf(lastFour[2]).intValue();
                    int y1 = Float.valueOf(lastFour[3]).intValue();
                    //Could be the wrong way round (x1,y1,x,y)?
                    Coords = CoordFiller.coordFillCalc(x, y, x1, y1);
                    String newCoOrds = "";
                    for (int j = 0; j < Coords.size(); j++)
                    {
                        newCoOrds += Coords.get(j) + " ";
                    }
                    newCoOrds.trim();
                    ClientStorage.storeAmmendedMotion(newCoOrds);

                }

            }
            ,time);
Run Code Online (Sandbox Code Playgroud)

Pét*_*rök 16

如果您使用的是Java5或更高版本,请考虑ScheduledThreadPoolExecutorFuture.使用前者,您可以安排在指定的延迟之后或以指定的时间间隔运行任务,从而Timer更可靠地接管角色.

Timer工具管理延迟("在100毫秒内运行此任务")和定期("每10毫秒运行此任务")任务的执行.但是,Timer有一些缺点,ScheduledThreadPoolExecutor应该被认为是它的替代品.[...]

A Timer只创建一个用于执行计时器任务的线程.如果定时器任务运行时间太长,其他TimerTasks 的定时精度可能会受到影响.如果重复TimerTask计划每10毫秒运行一次,另一个计划运行TimerTask40毫秒,则在长时间运行的任务完成后,重复执行的任务(取决于它是以固定速率还是固定延迟进行调度)会快速连续调用四次,或完全"错过"四次调用.计划的线程池通过允许您提供多个线程来执行延迟和定期任务来解决此限制.

另一个问题Timer是,如果TimerTask抛出未经检查的异常,它表现不佳.该Timer线程不捕获异常,所以从抛出的未经检查的异常TimerTask终止计时器线程.Timer在这种情况下也不会复活线程; 相反,它错误地认为整个Timer被取消了.在这种情况下,TimerTask已经调度但尚未执行的s永远不会运行,并且无法安排新任务.

Java Concurrency in Practice,第6.2.5节.

并且Futures 可以被限制为在指定时间内最多运行(TimeoutException如果无法及时完成则抛出一个).

更新

如果您不喜欢上述内容,可以让任务测量自己的执行时间,如下所示:

int totalTime = 50000; // in nanoseconds
long startTime = System.getNanoTime();
boolean toFinish = false;

while (!toFinish) 
{
    System.out.println("Task!");
    ...
    toFinish = (System.getNanoTime() - startTime >= totalTime);
}
Run Code Online (Sandbox Code Playgroud)