RxJava是否经常切换性能损失的线程?

时间:2018-04-20 01:52:10

标签: android performance websocket rx-java executors

REF:https://github.com/ReactiveX/RxAndroid/issues/420

在此背景下

//pseudo-code
websocket(Callback(data){
     //websocket very frequent data in no main thread
     Observable.just(data)
                                .observeOn(Schedulers.computation())
                                .subscribe(data -> {
                                    //computation thread
                                    map2Obj(data);
                                });
});


//computation
void map2Obj(data){
    //....

    then change to main thread
}

------------------打击是ExecutorService实现模型----------------------- ------------

在此背景下:

//pseudo-code
 static ExecutorService mExecutorService;
    static {
        mExecutorService = Executors.newFixedThreadPool(8);
    }

websocket(Callback(data){
     //websocket very frequent data in no main thread。change to other compute thread to prevent block "the thread of getting data"。in run() execute map2Obj(data)
        mExecutorService.execute(new NewFixThread(str));
});


//computation
void map2Obj(data){
    //....

    then change to main thread
}

RxJava更好还是java Executors?为什么?

THX !!!!!

1 个答案:

答案 0 :(得分:1)

它可以根据需要随时切换线程。

不再。

您的示例代码将不断将数据从主线程移动到另一个线程。这将导致每次性能损失几微秒,具体取决于必须创建线程的方式和时间。

正如你告诉它的那样。