rx.Observable.sample()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(5.7k)|赞(0)|评价(0)|浏览(154)

本文整理了Java中rx.Observable.sample()方法的一些代码示例,展示了Observable.sample()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.sample()方法的具体详情如下:
包路径:rx.Observable
类名称:Observable
方法名:sample

Observable.sample介绍

[英]Returns an Observable that emits the most recently emitted item (if any) emitted by the source Observable within periodic time intervals.

Backpressure Support: This operator does not support backpressure as it uses time to control data flow. Scheduler: sample operates by default on the computation Scheduler.
[中]返回一个可观测项,该可观测项在周期性时间间隔内发射源可观测项最近发射的项目(如果有)。
背压支持:该操作员不支持背压,因为它使用时间来控制数据流。调度程序:默认情况下,示例在计算调度程序上运行。

代码示例

代码示例来源:origin: jhusain/learnrxjava

  1. public static void main(String args[]) {
  2. hotStream().sample(500, TimeUnit.MILLISECONDS).toBlocking().forEach(System.out::println);
  3. }

代码示例来源:origin: com.netflix.rxjava/rxjava-core

  1. /**
  2. * Returns an Observable that emits only the last item emitted by the source Observable during sequential
  3. * time windows of a specified duration.
  4. * <p>
  5. * This differs from {@link #throttleFirst} in that this ticks along at a scheduled interval whereas
  6. * {@link #throttleFirst} does not tick, it just tracks passage of time.
  7. * <p>
  8. * <img width="640" height="305" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/throttleLast.png" alt="">
  9. * <dl>
  10. * <dt><b>Backpressure Support:</b></dt>
  11. * <dd>This operator does not support backpressure as it uses time to control data flow.</dd>
  12. * <dt><b>Scheduler:</b></dt>
  13. * <dd>{@code throttleLast} operates by default on the {@code computation} {@link Scheduler}.</dd>
  14. * </dl>
  15. *
  16. * @param intervalDuration
  17. * duration of windows within which the last item emitted by the source Observable will be
  18. * emitted
  19. * @param unit
  20. * the unit of time of {@code intervalDuration}
  21. * @return an Observable that performs the throttle operation
  22. * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#sample-or-throttlelast">RxJava wiki: throttleLast</a>
  23. * @see <a href="https://github.com/Netflix/RxJava/wiki/Backpressure">RxJava wiki: Backpressure</a>
  24. * @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.sample.aspx">MSDN: Observable.Sample</a>
  25. * @see #sample(long, TimeUnit)
  26. */
  27. public final Observable<T> throttleLast(long intervalDuration, TimeUnit unit) {
  28. return sample(intervalDuration, unit);
  29. }

代码示例来源:origin: com.netflix.rxjava/rxjava-core

  1. return sample(intervalDuration, unit, scheduler);

代码示例来源:origin: com.netflix.rxjava/rxjava-core

  1. /**
  2. * Returns an Observable that emits the most recently emitted item (if any) emitted by the source Observable
  3. * within periodic time intervals.
  4. * <p>
  5. * <img width="640" height="305" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/sample.png" alt="">
  6. * <dl>
  7. * <dt><b>Backpressure Support:</b></dt>
  8. * <dd>This operator does not support backpressure as it uses time to control data flow.</dd>
  9. * <dt><b>Scheduler:</b></dt>
  10. * <dd>{@code sample} operates by default on the {@code computation} {@link Scheduler}.</dd>
  11. * </dl>
  12. *
  13. * @param period
  14. * the sampling rate
  15. * @param unit
  16. * the {@link TimeUnit} in which {@code period} is defined
  17. * @return an Observable that emits the results of sampling the items emitted by the source Observable at
  18. * the specified time interval
  19. * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#sample-or-throttlelast">RxJava wiki: sample</a>
  20. * @see <a href="https://github.com/Netflix/RxJava/wiki/Backpressure">RxJava wiki: Backpressure</a>
  21. * @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.sample.aspx">MSDN: Observable.Sample</a>
  22. * @see #throttleLast(long, TimeUnit)
  23. */
  24. public final Observable<T> sample(long period, TimeUnit unit) {
  25. return sample(period, unit, Schedulers.computation());
  26. }

代码示例来源:origin: leeowenowen/rxjava-examples

  1. @Override
  2. public void run() {
  3. final Subscription subscription = Observable.create(new Observable.OnSubscribe<Integer>() {
  4. @Override
  5. public void call(Subscriber<? super Integer> subscriber) {
  6. for (int i = 0; i < 10; i++) {
  7. subscriber.onNext(i);
  8. sleep(100);
  9. }
  10. }
  11. })
  12. .subscribeOn(Schedulers.newThread())
  13. .sample(1, TimeUnit.SECONDS)
  14. .subscribe(new Action1<Integer>() {
  15. @Override
  16. public void call(Integer integer) {
  17. log(integer);
  18. }
  19. });
  20. AsyncExecutor.SINGLETON.schedule(new Runnable() {
  21. @Override
  22. public void run() {
  23. if (!subscription.isUnsubscribed()) {
  24. subscription.unsubscribe();
  25. }
  26. }
  27. }, 3, TimeUnit.SECONDS);
  28. }
  29. });

代码示例来源:origin: nurkiewicz/rxjava-book-examples

  1. @Test
  2. public void sample_64() throws Exception {
  3. Observable<Long> obs = Observable.interval(20, MILLISECONDS);
  4. //equivalent:
  5. obs.sample(1, SECONDS);
  6. obs.sample(Observable.interval(1, SECONDS));
  7. }

代码示例来源:origin: nurkiewicz/rxjava-book-examples

  1. @Test
  2. public void sample_25() throws Exception {
  3. Observable<String> delayedNames = delayedNames();
  4. delayedNames
  5. .sample(1, SECONDS)
  6. .subscribe(System.out::println);
  7. }

代码示例来源:origin: nurkiewicz/rxjava-book-examples

  1. @Test
  2. public void sample_170() throws Exception {
  3. Observable
  4. .range(0, Integer.MAX_VALUE)
  5. .map(Picture::new)
  6. .distinct()
  7. .distinct(Picture::getTag)
  8. .sample(1, TimeUnit.SECONDS)
  9. .subscribe(System.out::println);
  10. }

代码示例来源:origin: nurkiewicz/rxjava-book-examples

  1. @Test
  2. public void sample_9() throws Exception {
  3. long startTime = System.currentTimeMillis();
  4. Observable
  5. .interval(7, MILLISECONDS)
  6. .timestamp()
  7. .sample(1, SECONDS)
  8. .map(ts -> ts.getTimestampMillis() - startTime + "ms: " + ts.getValue())
  9. .take(5)
  10. .subscribe(System.out::println);
  11. }

代码示例来源:origin: nurkiewicz/rxjava-book-examples

  1. @Test
  2. public void sample_37() throws Exception {
  3. Observable<String> delayedNames = delayedNames();
  4. delayedNames
  5. .concatWith(delayedCompletion())
  6. .sample(1, SECONDS)
  7. .subscribe(System.out::println);
  8. }

相关文章

Observable类方法