forked from PacktPublishing/Learning-RxJava-Second-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh8_17.java
More file actions
32 lines (28 loc) · 973 Bytes
/
Copy pathCh8_17.java
File metadata and controls
32 lines (28 loc) · 973 Bytes
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
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.util.concurrent.ThreadLocalRandom;
public class Ch8_17 {
public static void main(String[] args) {
randomGenerator(1, 10000)
.subscribeOn(Schedulers.computation())
.doOnNext(i -> System.out.println("Emitting " + i))
.observeOn(Schedulers.io())
.subscribe(i -> {
sleep(50);
System.out.println("Received " + i);
});
sleep(10000);
}
static Flowable<Integer> randomGenerator(int min, int max) {
return Flowable.generate(emitter ->
emitter.onNext(ThreadLocalRandom.current().nextInt(min, max))
);
}
private static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}