对于我正在写的一些代码,我可以在Java中使用一个很好的通用实现debounce
。在Java中实现去抖动
public interface Callback {
public void call(Object arg);
}
class Debouncer implements Callback {
public Debouncer(Callback c, int interval) { ... }
public void call(Object arg) {
// should forward calls with the same arguments to the callback c
// but batch multiple calls inside `interval` to a single one
}
}
当call()
被多次调用在interval
毫秒具有相同参数的回调函数应调用一次。
的可视化:
Debouncer#call xxx x xxxxxxx xxxxxxxxxxxxxxx
Callback#call x x x (interval is 2)
- 不(像)这个已经存在的一些Java标准库?
- 你将如何实现?
看起来像[java.util.concurrency](http://download.oracle.com/javase/1.5 .0/docs/api/java/util/concurrent/package-summary.html)提供了积木 – levinalex 2011-01-20 00:38:35
我知道这是一个老问题,但几个月前我在这里发布了一个类似的问题:http:// stackoverflow。 COM /问题/ 18723112 /取消法通话-WH en-the-same-method-is-called-multiple-time/18758408#18758408并在GitHub上提供了一个可能有兴趣的可重用实现 – ARRG 2014-01-07 07:41:08