RxJava 全国卷

作者: Android那些事儿 | 来源:发表于2020-07-29 16:54 被阅读0次

    上岗先考试。

    道路千万条,“线程安全” 第一条。行车不规范,CaseStudy 两行泪!!!


    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            rxJavaExamination();
        }
    });
    
    private void rxJavaExamination() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                Log.d("hlwang", "111 isMainThread : " + (Thread.currentThread() == Looper.getMainLooper().getThread()));
                emitter.onNext("第一级主题");
                emitter.onComplete();
            }
        })
        .subscribeOn(AndroidSchedulers.mainThread())
        .doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                Log.d("hlwang", "222 isMainThread : " + (Thread.currentThread() == Looper.getMainLooper().getThread()));
            }
        })
        .observeOn(Schedulers.io())
        .doOnNext(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                Log.d("hlwang", "333 isMainThread : " + (Thread.currentThread() == Looper.getMainLooper().getThread()));
            }
        })
        .subscribeOn(Schedulers.newThread())
        .doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                Log.d("hlwang", "444 isMainThread : " + (Thread.currentThread() == Looper.getMainLooper().getThread()));
            }
        })
        .observeOn(Schedulers.io())
        .subscribe(new Observer() {
    
            @Override
            public void onSubscribe(Disposable d) {
                Log.d("hlwang", "555 isMainThread : " + (Thread.currentThread() == Looper.getMainLooper().getThread()));
            }
    
            @Override
            public void onNext(Object o) {
                Log.d("hlwang", "666 isMainThread : " + (Thread.currentThread() == Looper.getMainLooper().getThread()));
            }
    
            @Override
            public void onError(Throwable e) {
    
            }
    
            @Override
            public void onComplete() {
    
            }
        });
    }
    

    A :

    555 isMainThread : true
    444 isMainThread : true
    222 isMainThread : false
    111 isMainThread : true
    333 isMainThread : false
    666 isMainThread : false
    

    B :

    111 isMainThread : true
    222 isMainThread : false
    333 isMainThread : false
    444 isMainThread : true
    555 isMainThread : true
    666 isMainThread : false
    

    C :

    444 isMainThread : true
    555 isMainThread : true
    222 isMainThread : false
    111 isMainThread : true
    333 isMainThread : false
    666 isMainThread : false
    

    D :

    111 isMainThread : true
    222 isMainThread : false
    444 isMainThread : true
    555 isMainThread : true
    333 isMainThread : false
    666 isMainThread : false
    

    E :

    以上,无正确答案,我要评论正确答案
    

    相关文章

      网友评论

        本文标题:RxJava 全国卷

        本文链接:https://www.haomeiwen.com/subject/nlckrktx.html