Skip to content

Instantly share code, notes, and snippets.

@mox601
Created March 14, 2017 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mox601/964e2e7d9110f9f81f2265d75f74c3ee to your computer and use it in GitHub Desktop.
Save mox601/964e2e7d9110f9f81f2265d75f74c3ee to your computer and use it in GitHub Desktop.
@Test
public void throttlingRxJava() throws Exception {
Observable<Long> everySecond = Observable.fromIterable(Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L))
.map(aLong -> {
try {
Thread.sleep(1_000L);
} catch (InterruptedException e) {
//
}
return aLong;
});
Observable<Long> everyHundredMillis = Observable.interval(1, TimeUnit.MILLISECONDS);
everyHundredMillis
.map(a -> System.nanoTime())
.zipWith(everySecond, new BiFunction<Long, Long, Tuple2<Long, Long>>() {
@Override
public Tuple2<Long, Long> apply(Long aLong, Long aLong2) throws Exception {
return Tuples.of(aLong, aLong2);
}
})
.subscribe(aLong -> log.info(aLong + ""));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment