Skip to content

Instantly share code, notes, and snippets.

@CoderJava
Created October 1, 2018 15:08
Show Gist options
  • Select an option

  • Save CoderJava/ec4f5f813d2dce663aa561588ac81a63 to your computer and use it in GitHub Desktop.

Select an option

Save CoderJava/ec4f5f813d2dce663aa561588ac81a63 to your computer and use it in GitHub Desktop.
Interval Operator RxJava
public class RxOperatorInterval {
public static void main(String[] args) throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
Observable.interval(1000 * 2, TimeUnit.MILLISECONDS)
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
System.out.println("onSubscribe");
}
@Override
public void onNext(Long count) {
String now = new SimpleDateFormat("HH:mm:ss", Locale.US).format(new Date());
System.out.println(now + " onNext: " + count);
if (count == 5) {
onComplete();
}
}
@Override
public void onError(Throwable e) {
System.out.println("onError");
}
@Override
public void onComplete() {
System.out.println("onComplete");
countDownLatch.countDown();
}
});
countDownLatch.await();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment