Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java rxjava中的ThrottleTest和ThrottleList有什么区别?

在Observable中,有两种方法称为throttleLast和throttleLatest

大理石图相似,但两个内部代码不同

public final Observable<T> throttleLast(long intervalDuration, TimeUnit unit) {
    return sample(intervalDuration, unit);
}


public final Observable<T> sample(long period, TimeUnit unit, Scheduler scheduler, boolean emitLast) {
    ObjectHelper.requireNonNull(unit, "unit is null");
    ObjectHelper.requireNonNull(scheduler, "scheduler is null");
    return RxJavaPlugins.onAssembly(new ObservableSampleTimed<T>(this, period, unit, scheduler, emitLast));
}
public final Observable<T> throttleLatest(long timeout, TimeUnit unit, Scheduler scheduler, boolean emitLast) {
    ObjectHelper.requireNonNull(unit, "unit is null");
    ObjectHelper.requireNonNull(scheduler, "scheduler is null");
    return RxJavaPlugins.onAssembly(new ObservableThrottleLatest<T>(this, timeout, unit, scheduler, emitLast));
}

他们之间有什么区别


共 (2) 个答案

  1. # 1 楼答案

    参见@Slaw和@akarnokd的评论

    Not positive, but throttleLast might operate on fixed time intervals and throttleLatest resets the timeout whenever an item arrives. In other words, the difference between fixed rate and fixed delay.

    It's in the javadoc: throttleLatest "If no items were emitted from the upstream during this timeout phase, the next upstream item is emitted immediately and the timeout window starts from then.". ThrottleLast emits at a fixed rate and if there is no item, nothing is emitted.

    我不太明白,所以我试着比较自己

    sample code

    sample

  2. # 2 楼答案

    他们非常相似。 他们都有固定的时间窗口(上面的评论是错误的)

    唯一的区别是如何处理第一项

    • ThrottleLast不会立即发出第一个项目,它将始终等待时间窗口通过,然后从该时间窗口发出最后一个值(如果有)
    • ThrottleLatest将立即发出第一个项,之后它的行为将与ThrottleLast相同