ReactiveX / RxAndroid

RxJava bindings for Android
Apache License 2.0
19.89k stars 2.94k forks source link

Does RxJava frequently switch threads with performance loss? #420

Closed scsfwgy closed 6 years ago

scsfwgy commented 6 years ago

like this:

while(true){
    Thread.sleep(100);

   Observable.just(data)
                    .observeOn(Schedulers.computation())
                    .subscribe(data -> {
                        //compute....
                        ....
                    });
}
akarnokd commented 6 years ago

This question is not RxAndroid related. Switching threads unnecessarily is always a performance loss and RxJava does its best to avoid that. It really depends on what flow you have assembled in what context.

scsfwgy commented 6 years ago

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
}

thx!

JakeWharton commented 6 years ago

Please ask questions on StackOverflow or the RxJava mailing list.