【问题标题】:What difference between throttleLatest and throttleLast in rxjava?rxjava中throttleLatest和throttleLast有什么区别?
【发布时间】:2019-05-27 09:55:52
【问题描述】:

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

它们有什么区别?

【问题讨论】:

  • 不是肯定的,但throttleLast 可能会以固定的时间间隔运行,并且throttleLatest 会在项目到达时重置超时。也就是说,fixed ratefixed delay的区别。
  • 它在 javadoc 中:throttleLatest “如果在此超时阶段没有从上游发出任何项目,则立即发出下一个上游项目,并且超时窗口从那时开始。”。 ThrottleLast 以固定速率发射,如果没有项目,则不发射任何内容。

标签: java rx-java2 reactivex


【解决方案1】:

参见 @Slaw 和 @akarnokd 的 cmets。

不是肯定的,但throttleLast 可能会在固定的时间间隔内运行,并且throttleLatest 重置超时 每当项目到达时。换句话说,固定速率和固定延迟的区别

它在 javadoc 中:throttleLatest “如果在此超时阶段没有从上游发出任何项目,则立即发出下一个上游项目并从那时开始超时窗口。”。 ThrottleLast 以固定速率发射,如果没有项目,则不发射任何内容。

我不是很了解,所以我试着比较一下自己。

sample code

【讨论】:

  • 好的,实际上的区别是什么?这两列有何不同?
  • @mate00 throttleLatest 没有延迟。每当有项目到达时,throttleLatest 都会重置超时。
【解决方案2】:

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

唯一的区别是第一项的处理方式。

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

【讨论】:

    猜你喜欢
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 2010-10-02
    相关资源
    最近更新 更多