📅  最后修改于: 2020-11-12 05:27:24             🧑  作者: Mango
MayBe类表示延迟的响应。可能可观察可发出单个成功值或没有任何值。
以下是io.reactivex.Single
public abstract class Maybe
extends Object
implements MaybeSource
以下是MayBe Observable操作的顺序协议-
onSubscribe (onSuccess | onError | OnComplete)?
使用您选择的任何编辑器(例如,C:\> RxJava)创建以下Java程序。
import java.util.concurrent.TimeUnit;
import io.reactivex.Maybe;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableMaybeObserver;
import io.reactivex.schedulers.Schedulers;
public class ObservableTester {
public static void main(String[] args) throws InterruptedException {
//Create an observer
Disposable disposable = Maybe.just("Hello World")
.delay(2, TimeUnit.SECONDS, Schedulers.io())
.subscribeWith(new DisposableMaybeObserver() {
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onSuccess(String value) {
System.out.println(value);
}
@Override
public void onComplete() {
System.out.println("Done!");
}
});
Thread.sleep(3000);
//start observing
disposable.dispose();
}
}
使用javac编译器编译类,如下所示:
C:\RxJava>javac ObservableTester.java
现在如下运行ObservableTester-
C:\RxJava>java ObservableTester
它应该产生以下输出-
Hello World