반응형
timer
호출 시점부터 지정시간 동안 대기한 후 long type 숫자 0 하나만 통지하고 종료하는 Flowable/Observable을 생성하는 연산자이다.
timer 메서드는 기본적으로 Schedulers.computation() 스케줄러의해 실행된다.
[소스코드]
import io.reactivex.Flowable;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws Exception {
tiner();
}
static void tiner() throws Exception {
Flowable<Long> flowable = Flowable.timer(1000L, TimeUnit.MILLISECONDS);
flowable.subscribe(new PrintSubscriber<>());
Thread.sleep(5000L);
}
}
[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);
}
}
[결과]
RxComputationThreadPool-1 : 0
RxComputationThreadPool-1 : Complete
반응형
'Language > Java' 카테고리의 다른 글
[RxJava] empty (0) | 2023.04.26 |
---|---|
[RxJava] defer (0) | 2023.04.26 |
[RxJava] interval (0) | 2023.04.26 |
[RxJava] range, rangeLong (0) | 2023.04.26 |
[RxJava] fromCallable (0) | 2023.04.26 |