These days I have seen an article about RxJava thread switching. When it comes to RxJava thread switching, it will definitely involve ObserveOn and SubscribeOn methods.

There are many articles on the Internet about the call order and call effectiveness of ObserveOn and SubscribeOn, and readers can search by themselves.

Fat man is just trying to deepen the understanding of RxJava thread switching, and also to improve some of the online conclusions are not rigorous, after all, practice makes truth.

Here is only the conclusion, for the principle, will be another, introduce RxJava principle mechanism, detailed introduction.

First, let’s talk about what ObserveOn and SubscribeOn do. See the description and figure on the official website

As shown in this illustration, the SubscribeOn operator designates which thread the 
Observable will begin operating on, no matter at what point in the chain of operators 
that operator is called. ObserveOn, on the other hand, affects the thread that the 
Observable will use below where that operator appears. For thisreason, you may call ObserveOn multiple times at various points during the chain of Observable operators in order to change on which threads certain of those operators operate. As shown in the figure, the SubscribeOn operator specifies on which thread an Observable will start operating, regardless of where in the operator chain the operator is called. ObserveOn, on the other hand, affects the thread that an Observable will use where the operator appears. Therefore, you can make multiple calls to ObserveOn at various points in the Observable operator chain to change which threads certain operators operate on. Conclusion:1.SubscribeOn switches the subscribed thread2.ObserveOn toggles the sending thread.1.SubscribeOn switches the thread executed by the Observable2.ObserveOn changes the Observable thread to be simple and crude:1.Before calling ObserveOn, switch the threads for time-consuming operations such as network requests and IO.2.After the time-consuming operation, ObserveOn switches back and updates the UI.Copy the code

About subscribeOn

activityMainBinding.button.setOnClickListener(v->{
            Observable.just(3)// Pass a 3
                    .subscribeOn(Schedulers.newThread())// Notice the 1 here
                    .map(str->{
                        Log.e(TAG, "First map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribeOn(Schedulers.newThread())// Note here 2
                    .map(str->{
                        Log.e(TAG, "Second map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribeOn(Schedulers.newThread())// Note here 3
                    .map(str->{
                        Log.e(TAG, "Third map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribe(integer->{
                Log.e(TAG,"The last received is" + integer+Current thread ="+Thread.currentThread().getName());
            });
        });
Copy the code

Let’s look at another one

activityMainBinding.button.setOnClickListener(v->{
            Observable.just(3)// Pass a 3
                    .subscribeOn(Schedulers.newThread())// Notice the 1 here
                    .map(str->{
                        Log.e(TAG, "First map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribeOn(Schedulers.io())// Note here 2
                    .map(str->{
                        Log.e(TAG, "Second map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())// Note here 3
                    .map(str->{
                        Log.e(TAG, "Third map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribe(integer->{
                Log.e(TAG,"The last received is" + integer+Current thread ="+Thread.currentThread().getName());
            });
        });
Copy the code

Come again

activityMainBinding.button.setOnClickListener(v->{
            Observable.just(3)// Pass a 3
                    .subscribeOn(Schedulers.newThread())
                    .map(str->{
                        Log.e(TAG, "First map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribeOn(Schedulers.newThread())
                    .map(str->{
                        Log.e(TAG, "Second map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .map(str->{
                        Log.e(TAG, "Third map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribe(integer->{
                Log.e(TAG,"The last received is" + integer+Current thread ="+Thread.currentThread().getName());
            });
        });
Copy the code

Come again

activityMainBinding.button.setOnClickListener(v->{
            Observable.just(3)// Pass a 3
                    .subscribeOn(Schedulers.io())
                    .map(str->{
                        Log.e(TAG, "First map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribeOn(Schedulers.io())
                    .map(str->{
                        Log.e(TAG, "Second map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribeOn(Schedulers.io())
                    .map(str->{
                        Log.e(TAG, "Third map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                        return Math.pow(str,2);
                    })
                    .subscribe(integer->{
                Log.e(TAG,"The last received is" + integer+Current thread ="+Thread.currentThread().getName());
            });
        });
Copy the code

For subscribeOn

Different calls, keep only the first call.

Such as:

SubscribeOn (Schedulers. NewThread ()) / / reservations

.subscribeOn(AndroidSchedulers.mainThread())

Schedulers.io() is called multiple times. The number of calls is 2 and the number is 1.

Such as:

.subscribeon (schedulers.io ()) switches to rxCachedThreadScheduler-1

or

.subscribeOn(Schedulers.io())

.subscribeon (schedulers.io ()) switches to rxCachedThreadScheduler-2

or

.subscribeOn(Schedulers.io())

.subscribeOn(Schedulers.io())

.subscribeon (schedulers.io ()) switches to rxCachedThreadScheduler-1

. In order to push

Schedulers.newthread () is called multiple times, stack.

Such as:

.subscribeOn(Schedulers.newThread())

.subscribeOn(Schedulers.newThread())

.subscribeon (schedulers.newthread ()) switches to rxNewThreadScheduler-3

About ObserveOn

activityMainBinding.button.setOnClickListener(v->{
    Observable.just(3)// Pass a 3
            .observeOn(Schedulers.newThread())// Switch for the first time
            .map(str->{
                Log.e(TAG, "First map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                return Math.pow(str,2);
            })
            .observeOn(Schedulers.io())// Switch the second time
            .map(str->{
                Log.e(TAG, "Second map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                return Math.pow(str,2);
            })
            .observeOn(AndroidSchedulers.mainThread())// Switch for the third time
            .map(str->{
                Log.e(TAG, "Third map, parameter ="+str+Current thread ="+Thread.currentThread().getName());
                return Math.pow(str,2);
            })
            .subscribe(integer->{
        Log.e(TAG,"The last received is" + integer+Current thread ="+Thread.currentThread().getName());
    });
});
Copy the code

ObserveOn is straightforward. It is called once and switched once.