Last active
August 29, 2015 14:04
-
-
Save benjchristensen/609ca956e28b99023533 to your computer and use it in GitHub Desktop.
SampleExample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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