-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample1.java
More file actions
42 lines (36 loc) · 1.17 KB
/
example1.java
File metadata and controls
42 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import rx.Observable;
import rx.Subscriber;
import java.util.ArrayList;
import java.util.List;
/**
* SECTION: Transforming Observables with Operators
*
* Created by matie on 26/04/15.
*/
public class example1 {
public static void main(String[] args){
//prints from 11 to 15
customObservableNonBlocking().skip(10).take(5)
.map(num -> num + 1)
.subscribe(modNum -> System.out.println(modNum));
}
//create our custom asynchronous observable
private static Observable<Integer> customObservableNonBlocking(){
return Observable.create(subscriber -> customAsyncSubscriber(subscriber));
}
private static void customAsyncSubscriber(Subscriber<? super Integer> subscriber) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 20 ; i++){
if(!subscriber.isUnsubscribed()){
subscriber.onNext(i);
}
}
if(!subscriber.isUnsubscribed())
subscriber.onCompleted();
}
});
thread.start();
}
}