Last active
August 29, 2015 14:04
-
-
Save benjchristensen/da031a55bb416812f1d0 to your computer and use it in GitHub Desktop.
WindowExample
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
// window(500, TimeUnit.MILLISECONDS) | |
999999999 | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
0 | |
1 | |
2 | |
3 | |
999999999 | |
0 | |
1 | |
2 | |
999999999 | |
0 | |
1 | |
2 | |
3 | |
999999999 | |
999999999 | |
0 | |
1 | |
2 | |
3 | |
999999999 | |
999999999 | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
0 | |
1 | |
999999999 | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
999999999 | |
0 | |
1 | |
2 | |
999999999 | |
// window(10) | |
999999999 | |
0 | |
1 | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
999999999 | |
8 | |
9 | |
0 | |
1 | |
0 | |
1 | |
2 | |
3 | |
4 | |
0 | |
999999999 | |
1 | |
0 | |
1 | |
2 | |
0 | |
1 | |
2 | |
3 | |
4 | |
0 | |
999999999 | |
1 | |
2 | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
999999999 | |
8 | |
9 | |
10 | |
11 | |
0 | |
1 | |
2 | |
3 | |
4 | |
0 | |
999999999 | |
1 | |
0 | |
1 | |
2 | |
3 | |
0 | |
0 | |
1 | |
2 | |
3 | |
999999999 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
0 | |
1 | |
2 | |
999999999 | |
3 | |
4 | |
5 | |
6 | |
7 | |
0 | |
1 | |
2 | |
3 | |
4 | |
999999999 | |
5 | |
6 | |
7 | |
8 | |
9 | |
0 | |
1 | |
0 | |
1 | |
2 | |
999999999 | |
0 | |
1 | |
2 | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 |
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 WindowExample { | |
public static void main(String args[]) { | |
// buffer every 500ms (using 999999999 to mark start of output) | |
hotStream().window(500, TimeUnit.MILLISECONDS).take(10).flatMap(w -> w.startWith(999999999)).toBlocking().forEach(System.out::println); | |
// buffer 10 items at a time (using 999999999 to mark start of output) | |
hotStream().window(10).take(10).flatMap(w -> w.startWith(999999999)).toBlocking().forEach(System.out::println); | |
} | |
/** | |
* This is an artificial source to demonstrate an infinite stream that bursts intermittently | |
*/ | |
public static Observable<Integer> hotStream() { | |
return Observable.create((Subscriber<? super Integer> s) -> { | |
while (!s.isUnsubscribed()) { | |
// burst some number of items | |
for (int i = 0; i < Math.random() * 20; i++) { | |
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() * 1000)); | |
} catch (Exception e) { | |
// do nothing | |
} | |
} | |
}).subscribeOn(Schedulers.newThread()); // use newThread since we are using sleep to block | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment