반응형
range, rangeLong
range 메서드는 지정한 시작 값부터 지정한 개수만큼 하나씩 순서대로 증가하는 Integer 값을 통지 Flowable/Observable을 생성 연산자이고 rangeLong은 Long값을 통지 Flowable/Observable을 생성 연산자이다.
ex)
시작 값이 3이고 통지하려는 데이터 수는 5이면 '3,4,5,6,7' 을 통지한다.
public class Main {
public static void main(String[] args) {
range();
rangeLong();
}
static void range() {
System.out.println("range");
Flowable<Integer> flowable = Flowable.range(3, 5);
flowable.subscribe(new PrintSubscriber<>());
}
static void rangeLong() {
System.out.println("rangeLong");
Flowable<Long> flowable = Flowable.rangeLong(3, 5);
flowable.subscribe(new PrintSubscriber<>());
}
}
[Subscribe 코드]
import io.reactivex.subscribers.DisposableSubscriber;
public class PrintSubscriber<T> extends DisposableSubscriber<T> {
private String label;
public PrintSubscriber() {
}
public PrintSubscriber(String label) {
this.label = label;
}
@Override
public void onNext(T data) {
String threadName = Thread.currentThread().getName();
String resultMsg = label == null ? (threadName + " : " + data) : (threadName + " : "+ label + " : "+ data);
System.out.println(resultMsg);
}
@Override
public void onError(Throwable throwable) {
String threadName = Thread.currentThread().getName();
String resultMsg = label == null ? (threadName + " : Exception=" + throwable) : (threadName + " : "+ label + " : "+ throwable);
System.out.println(resultMsg);
}
@Override
public void onComplete() {
String threadName = Thread.currentThread().getName();
String resultMsg = label == null ? (threadName + " : Complete") : (threadName + " : "+ label + " : Complete");
System.out.println(resultMsg);
}
}
[결과]
range
main : 3
main : 4
main : 5
main : 6
main : 7
main : Complete
rangeLong
main : 3
main : 4
main : 5
main : 6
main : 7
main : Complete
반응형
'Language > Java' 카테고리의 다른 글
[RxJava] timer (0) | 2023.04.26 |
---|---|
[RxJava] interval (0) | 2023.04.26 |
[RxJava] fromCallable (0) | 2023.04.26 |
[RxJava] fromArray, fromIterable (0) | 2023.04.26 |
[RxJava] Just (0) | 2023.04.26 |