Skip to content

Instantly share code, notes, and snippets.

@benjchristensen
Last active August 29, 2015 14:04
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 benjchristensen/609ca956e28b99023533 to your computer and use it in GitHub Desktop.
Save benjchristensen/609ca956e28b99023533 to your computer and use it in GitHub Desktop.
SampleExample
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;
public class SampleExample {
public static void main(String args[]) {
hotStream().sample(500, TimeUnit.MILLISECONDS).toBlocking().forEach(System.out::println);
}
/**
* This is an artificial source to demonstrate an infinite stream that emits randomly
*/
public static Observable<Integer> hotStream() {
return Observable.create((Subscriber<? super Integer> s) -> {
int i = 0;
while (!s.isUnsubscribed()) {
s.onNext(i++);
try {
// sleep for a random amount of time
// NOTE: Only using Thread.sleep here as an artificial demo.
Thread.sleep((long) (Math.random() * 100));
} catch (Exception e) {
// do nothing
}
}
}).subscribeOn(Schedulers.newThread());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment