Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Last active August 19, 2018 07:24
Show Gist options
  • Save anitaa1990/fc436dc276c15c9552b3df87f76f856e to your computer and use it in GitHub Desktop.
Save anitaa1990/fc436dc276c15c9552b3df87f76f856e to your computer and use it in GitHub Desktop.
/*
* We create two Observables: left & right which emits a value every 100 milliseconds.
* We join left Observable to the right Observable.
* The two integers emitted from both the Observables are added
* & the result is printed.
*
* */
Observable<Long> left = Observable
.interval(100, TimeUnit.MILLISECONDS);
Observable<Long> right = Observable
.interval(100, TimeUnit.MILLISECONDS);
left.join(right,
aLong -> Observable.timer(0, TimeUnit.SECONDS),
aLong -> Observable.timer(0, TimeUnit.SECONDS),
(l, r) -> {
System.out.println("Left result: " + l + " Right Result: " + r);
return l + r;
})
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Long aLong) {
System.out.println("onNext: " + aLong);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
Thread.sleep(1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment