-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepeatOperator.java
50 lines (42 loc) · 1.31 KB
/
RepeatOperator.java
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
43
44
45
46
47
48
49
50
package rxjava.creating;
import io.reactivex.Observable;
import io.reactivex.functions.BooleanSupplier;
import io.reactivex.functions.Consumer;
/**
* Author: andy.xwt
* Date: 2019/2/1 11:17
* Description:
* repeat:运算符允许重新发送一系列事件
* repeatUntil:直到某个条件满足才停止发送
*
* @see <a href="https://mcxiaoke.gitbooks.io/rxdocs/content/operators/Repeat.html"/>
*/
class RepeatOperator {
static void testRepeat() {
//repeat执行次数
Observable.just("1").repeat(3).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
System.out.println(s);
}
});
}
static void testRepeatUntil() {
Observable.just("2").repeatUntil(new BooleanSupplier() {
@Override
public boolean getAsBoolean() throws Exception {
int rand = (int) (Math.random() * 10);
return rand == 5;//直到等于5才停止
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
System.out.println(s);
}
});
}
public static void main(String[] args) {
// testRepeat();
testRepeatUntil();
}
}