Java 有没有更干净的方法来编写这个轮询循环?

Java 有没有更干净的方法来编写这个轮询循环?,java,loops,webdriver,polling,selenium-webdriver,Java,Loops,Webdriver,Polling,Selenium Webdriver,我正在用java编写Selenium/WebDriver中的自动测试用例。我实现了以下代码来轮询现有WebElements,但由于我不是Java专家,我想知道是否有更干净的方法来编写此方法: /** selects Business index type from add split button */ protected void selectBusinessLink() throws Exception { Calendar rightNow = Calend

我正在用java编写Selenium/WebDriver中的自动测试用例。我实现了以下代码来轮询现有WebElements,但由于我不是Java专家,我想知道是否有更干净的方法来编写此方法:

/** selects Business index type from add split button */
    protected void selectBusinessLink() throws Exception
    {
        Calendar rightNow = Calendar.getInstance();
        Calendar stopPolling = rightNow;
        stopPolling.add(Calendar.SECOND, 30);
        WebElement businessLink = null;
        while (!Calendar.getInstance().after(stopPolling))
        {
            try
            {
                businessLink = findElementByLinkText("Business");
                businessLink.click();
                break;
            }
            catch (StaleElementReferenceException e)
            {
                Thread.sleep(100);
            }
            catch (NoSuchElementException e)
            {
                Thread.sleep(100);
            }
            catch (ElementNotVisibleException e)
            {
                Thread.sleep(100);
            }
        }
        if (businessLink == null)
        {
            throw new SystemException("Could not find Business Link");
        }
    }
这一行让我觉得代码有点脏:

 while (!Calendar.getInstance().after(stopPolling))

你可以这样做

long t = System.currentMillis();   // actual time in milliseconds from Jan 1st 1970.
while (t > System.currentMillis() - 30000 )  {
   ...

以毫秒为单位使用系统时间如何

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 30);
long stopPollingTime = calendar.getTimeInMillis();
while (System.currentTimeMillis() < stopPollingTime) {
  System.out.println("Polling");
  try {
    Thread.sleep(100);
  } catch (InterruptedException e) {
  }
}
Calendar Calendar=Calendar.getInstance();
calendar.add(calendar.SECOND,30);
long stopPollingTime=calendar.getTimeInMillis();
while(System.currentTimeMillis()
只是一个注释
java.util.Calendar
是一个糟糕的接口和实现。如果可以的话,可以改用JodaTime,或者直接使用
longs
作为毫秒。<而不是>,但这正是我想要的。谢谢。