【问题标题】:JAVA: how to interrupt function if it last more than 1 secJAVA:如果持续超过1秒,如何中断功能
【发布时间】:2013-07-04 02:18:49
【问题描述】:

我的程序中有这样的代码

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

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

我该怎么做?

【问题讨论】:

  • func(int i) 是做什么的?您可以从此 sn-p 限制总体执行时间(在某种程度上),但如果在特定的 func(i) 调用中花费了太多时间,听起来您想限制单个调用并继续下一个调用。
  • 我不能在 func(i) 中做任何事情。它根据输入生成伪随机数字序列。还有其他方法吗?
  • 显示你的 func 方法
  • 无论func是什么,都可以使用自定义异常处理,如下图。

标签: java time interrupt


【解决方案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);
    }
}

【讨论】:

    【解决方案2】:

    中断可能需要很长时间的函数的一种方法是在单独的线程中运行它。然后,您可以在一秒钟后向该线程发送一条消息,告诉它停止。在不使用线程的情况下,您可以通过将 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;
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        如果您想在Thread 中运行您的func(i);,那就另当别论了。

        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;
            }
        }
        

        【讨论】:

          【解决方案5】:

          如果你不想要另一个线程,你可以将你的异常处理注入到 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() 注释掉即可。 玩得开心。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-02-08
            • 2012-08-07
            • 2014-02-27
            • 2023-03-18
            • 2015-11-14
            相关资源
            最近更新 更多