java – 在特定时间之后突破迭代的最佳方法是什么?

我正在迭代迭代器,其中hasNext()永远不会返回false.但是,在指定时间(比方说20秒)之后,我想停止迭代.问题是Iterator的next()方法是阻塞的,但即便如此,在指定的时间之后,我只需要迭代来停止.

这是我的示例Iterable和Iterator来模拟我的问题.

public class EndlessIterable implements Iterable<String> {
    static class EndlessIterator implements Iterator<String> {
         public boolean hasNext() { return true; }
         public String next() { 
             return "" + System.currentTimeMillis(); //in reality, this code does some long running task, so it's blocking
         }
    }
   public Iterator<String> iterator() { return new EndlessIterator(); }
}

这是我要测试的代码.

EndlessIterable iterable = new EndlessIterable();
for(String s : iterable) { System.out.println(s); }

我想将代码/逻辑放入Iterable类来创建一个Timer,因此在指定的时间结束后,将抛出异常以便停止迭代.

public class EndlessIterable implements Iterable<String> {
    static class EndlessIterator implements Iterator<String> {
        public boolean hasNext() { return true; }
        public String next() { 
            try { Thread.sleep(2000); } catch(Exception) { } //just sleep for a while
            return "" + System.currentTimeMillis(); //in reality, this code does some long running task, so it's blocking
        }
    }
    static class ThrowableTimerTask extends TimerTask {
        private Timer timer;
        public ThrowableTimerTask(Timer timer) { this.timer = timer; }
        public void run() {
            this.timer.cancel();
            throw new RuntimeException("out of time!");
        }
    }
    private Timer timer;
    private long maxTime = 20000; //20 seconds
    public EndlessIterable(long maxTime) {
        this.maxTime = maxTime;
        this.timer = new Timer(true);
    }
    public Iterator<String> iterator() { 
        this.timer.schedule(new ThrowableTimerTask(this.timer), maxTime, maxTime);
        return new EndlessIterator();
    }
}

然后我尝试按如下方式测试此代码.

EndlessIterable iterable = new EndlessIterable(5000);
try {
    for(String s : iterable) { System.out.println(s); }
} catch(Exception) {
    System.out.println("exception detected: " + e.getMessage());
}
System.out.println("done");

我注意到的是,在时间结束后抛出RuntimeException,但是,

> for循环继续,
>永远不会到达catch块,并且
>我从未到达代码的末尾(完成打印).

我已经描述过解决这个问题的任何策略,方法或设计模式?

请注意

>在我的实际代码中,我无法控制Iterator
>我只能控制Iterable和实际迭代

最佳答案 你正在使用错误的工具来完成工作.如果要使操作超时,则必须将检查添加到操作中.建议将普通迭代器逻辑与超时检查分开,这似乎符合您的说法,即您无法更改Iterator实现.为此,使用装饰器/委托模式:

// an iterator wrapping another one adding the timeout functionality
class TimeOutIterator<T> implements Iterator<T> {
  final Iterator<T> source;
  final long deadline;

  public TimeOutIterator(Iterator<T> dataSource, long timeout, TimeUnit unit) {
    source=dataSource;
    deadline=System.nanoTime()+unit.toNanos(timeout);
  }
  private void check() {
    if(System.nanoTime()-deadline >= 0)
      throw new RuntimeException("timeout reached");
  }
  public boolean hasNext() {
    check();
    return source.hasNext();
  }
  public T next() {
    check();
    return source.next();
  }
  public void remove() {
    check();
    source.remove();
  }
}

所以你可以实现你的iterable:

public class EndlessIterable implements Iterable<String> {
  static class EndlessIterator implements Iterator<String> {
   public boolean hasNext() { return true; }
   public String next() { 
     // dummy code illustrating the long running task
     try { Thread.sleep(2000); } catch(Exception e) { }
     return "" + System.currentTimeMillis();
   }
   public void remove() { throw new UnsupportedOperationException(); }
  }
  private long maxTime;
  private TimeUnit unit;

  public EndlessIterable(long maxTime, TimeUnit timeUnit) {
    this.maxTime = maxTime;
    this.unit = timeUnit;
  }
  public Iterator<String> iterator() { 
    return new TimeOutIterator<>(new EndlessIterator(), maxTime, unit);
  }
}

然后测试代码如下:

// should timeout after five seconds
EndlessIterable iterable = new EndlessIterable(5, TimeUnit.SECONDS);
try {
 for(String s : iterable) { System.out.println(s); }
} catch(Exception e) {
  System.out.println("exception detected: " + e);
}
System.out.println("done");
点赞