1

我的程序中有这样的代码

for(int i = 0; i < 100000; i++) {
    func(i);
}

对于 i 的大多数值,func 持续不到 1 秒,但对于某些值,它可能会持续几分钟,所以如果持续时间过长,我需要中断它。

我怎样才能做到这一点?

4

5 回答 5

1

FutureTask 非常适合执行超时代码。

    FutureTask task = new FutureTask(new Callable() {
        @Override
        public Object call() throws Exception {
            /* Do here what you need */
            return null; /* Or any instance */
        }
    }) {
    };
    try {
        Object result = task.get(1, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        Logger.getLogger(Example1.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ExecutionException ex) {
        Logger.getLogger(Example1.class.getName()).log(Level.SEVERE, null, ex);
    } catch (TimeoutException ex) {
        Logger.getLogger(Example1.class.getName()).log(Level.SEVERE, null, ex);
    }
}
于 2013-07-03T17:58:44.533 回答
0

中断可能需要太长时间的函数的一种方法是在单独的线程中运行它。然后,您可以在一秒钟后向该线程发送一条消息,告诉它停止。在不使用线程的情况下,您可以通过将 func 替换为以下代码来处理它:

function process(int i, long maxMiliseconds) {
    long start = System.currentTimeMillis();
    while(System.currentTimeMillis() - start < maxMiliseconds) {
        //do your processing one step at a time
        // return an answer if you have one.
    }
    //make some record of the fact that the process timed out for i.
    return;
}
于 2013-07-03T16:17:24.343 回答
0

如果你不想要另一个线程,你可以将你的异常处理注入到 func 中。

  public static void main(String[] args)
{


        try 
        {
           func(1);
        } 
        catch (timeR e) 
        {
             System.out.println("enough!")
             // TODO Auto-generated catch block
             e.printStackTrace();

        }

}


    static void func(int a) throws timeR //your func needs to throw something when time-is-up
    {
       long time1,time2;             //reference time and current time
       time1=System.nanoTime();      //having reference
       time2=System.nanoTime();      //init of current
       while(true)                   //You did not put your func, so I used inf-while
       {
            //here whatever your func does,
                //.....
                //.....
            //below line checks if time is up
            time2=System.nanoTime();
            if((time2-time1)>1000000000)   //1000000000 is 1 second or 1Billion nanoseconds
            { throw new timeR("time is up!");} 
            //this is throwing(an alternative exit from this function)
       }

    }

    static class timeR extends Exception
    {
      //Parameterless Constructor

       public timeR() 
       {

       }

       //Constructor that accepts a message
       public timeR(String message)
       {
          super(message);
       }
    }

输出:func() 调用后 1 秒:

enough!
proje.lineerCebir$timeR: time is up!
at proje.lineerCebir.func(lineerCebir.java:198)
at proje.lineerCebir.main(lineerCebir.java:179)

也许您不想看到红色消息,那么只需将 e.printStackTrace() 注释掉即可。 玩得开心。

于 2013-07-03T17:12:10.850 回答
0

您可以在单独的线程中启动 func() ,然后join(long millis)在线程上执行方法以等待 1 秒以使其结束。但是线程仍然会运行直到它完成(stop()不推荐使用方法)。这样做的方法是控制您当前的线程并做出适当的反应

于 2013-07-03T16:27:04.387 回答
0

这就是我的看法。我确信有一些方法可以用更少的代码行来完成,但这是一个简单的解决方案

如果您想参加比赛func(i);Thread那将是另一回事。

public class MainClass {
    private static boolean riding, updated = false;


    private static int timeout = 10000;

    public static void main(String[] args) {
        while (true) {
            if (!riding){
                long endTimeMillis = System.currentTimeMillis() + timeout;
                func(endTimeMillis);
            }
        }
    }

    private static void func(long endTimeMillis) {
        for (int i = 0; i < 9999; i++) {
            if ((!riding && System.currentTimeMillis() < endTimeMillis) || updated) {
                updated = false;
                System.out.println("run method main function");
                riding = true;
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis() + " > "
                        + endTimeMillis);
                if (System.currentTimeMillis() > endTimeMillis) {
                    updated = true;
                    System.out.println("overdue");
                    riding = false;
                    break;
                }
            }
        }
        riding = false;
    }
}
于 2013-07-03T17:00:50.930 回答