Does RxJava frequently switch threads with performance loss?

226 views Asked by At

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

in this context:

//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
}

------------------the blow is ExecutorService implementation model-----------------------------------

in this context:

//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 is better or java Executors?why?

Thx!!!!!

1

There are 1 answers

2
Bob Dalgleish On

It switches threads as often as you need it to.

And no more.

Your sample code will continually move data from the main thread to another thread. This will cause a performance loss of a few microseconds each time, depending on how and when threads have to be created.

Exactly as you told it to.