Doonsubscribe run threading Analysis by default in Rxjava

Source: Internet
Author: User

If you are not aware of rxjava1.x, you can refer to the following articles.

1. Rxjava Usage Introduction "video tutorial"
2. Rxjava operator
? Creating observables (observable creation Operator) "video tutorial"
? Transforming observables (observable's conversion Operator) "video tutorial"
? Filtering observables (observable filter Operator) "video tutorial"
? Combining observables (observable's combined Operator) "video tutorial"
? Error handling Operators (observable operator) "video tutorial"
? Observable Utility Operators (auxiliary operator for Observable) "video tutorial"
? Conditional and Boolean Operators (conditions and Boolean operators for Observable) "video tutorial"
? Mathematical and Aggregate Operators (observable mathematical operations and aggregation Operators) "video tutorial"
? Other such as Observable.tolist (), observable.connect (), observable.publish () and so On. "video tutorial"
3. RxJava observer's relationship with Subcriber "video tutorial"
4. Rxjava Line Program Control (Scheduler) "video tutorial"
5. RxJava Concurrent data stream launch too fast how to do (back pressure (backpressure)) "video tutorial"

Objective

In the classroom "rxjava tour" There are students message: map and Doonsubscribe default scheduler is the IO scheduler, here is wrong?

Below we analyze.

In the previous subscriber, it was mentioned that Subscriber's OnStart () could be used to initialize the process before it Started. however, OnStart () cannot specify a thread because it was called when subscribe () occurred. instead, the thread can only run when subscribe () is Called. This leads to the assumption that OnStart () contains code for line threads requirements (such as displaying a ProgressBar on the interface, which must be run on the main thread), and that there is a risk that the thread will be illegal, since sometimes you cannot predict where subscribe () will Run.

corresponding to the Subscriber.onstart (). There is a method Observable.doonsubscribe ().

It is the same as Subscriber.onstart () after the subscribe () call and runs before the event is Sent. But the difference is that it can specify a thread.

By default, Doonsubscribe () runs on a thread that occurs in subscribe (), and assuming that there is Subscribeon () after Doonsubscribe (), it will run on the thread specified by its recent Subscribeon ().

Demo Sample Code:

observable.create  ( Onsubscribe) .subscribeon  (schedulers.io  ()) .doonsubscribe  (new Action0 () {@Override public void call  () {progressbar.setvisibility  (View< Span class= "hljs-preprocessor". VISIBLE ) }) .subscrib EOn  (androidschedulers.mainthread  ())//specify main thread .observeon  (androidschedulers.mainthread  ()) Span class= "hljs-preprocessor" >.subscribe  (subscriber) ;   

Subscribeon the creation of the Observable before the operator relabeled and the Doonsubscribe operator, in other words doonsubscribe and Observable's creation operators are always followed by their recent subscrib EOn Control. It doesn't matter, look at the following code you UNDERSTAND.

Observable.create(new Observable.OnSubscribe<Integer>() { @Override public void call(Subscriber<?

SuperInteger> Subscriber) {subscriber.onnext (1); Subscriber.oncompleted (); }}). Doonsubscribe (NewAction0 () {@Override public void Pager() {System.out.println ("00doOnSubscribe thread"+ thread.currentthread (). getName () +"medium"); }}). Subscribeon (schedulers.newthread ()). Map (Newfunc1<integer, string> () {@Override publicStringPager(integer Integer) {System.out.println ("map1 thread"+ thread.currentthread (). getName () +"medium");returnInteger +""; }}). Doonsubscribe (NewAction0 () {@Override public void Pager() {System.out.println ("11doOnSubscribe thread"+ thread.currentthread (). getName () +"medium"); }}). subscribeon (schedulers.io ()). observeon (schedulers.io ()). Map (Newfunc1<string, string> () {@Override publicStringPager(String S) {System.out.println ("MAP2 thread"+ thread.currentthread (). getName () +"medium");returnS +"1"; }}). Doonsubscribe (NewAction0 () {@Override public void Pager() {System.out.println ("22doOnSubscribe thread"+ thread.currentthread (). getName () +"medium"); }}). Subscribeon (schedulers.newthread ()). Subscribe (NewSubscriber<string> () {@Override public void oncompleted() { }@Override public void OnError(throwable E) { }@Override public void OnNext(String S) {System.out.println ("onnext thread"+ thread.currentthread (). getName () +"medium"); } });

Running results such as the following:

22doOnSubscribe在线程RxNewThreadScheduler-1中11doOnSubscribe在线程RxIoScheduler-3中00doOnSubscribe在线程RxNewThreadScheduler-2中map1在线程RxNewThreadScheduler-2中map2在线程RxIoScheduler-2中onNext在线程RxIoScheduler-2中

According to the code and running results I summarize for example the Following:

    1. Doonsubscribe () is similar to OnStart (), and is called back at the time of code Invocation. however, doonsubscribe () can change the running thread through the Subscribeon () operator, and the sooner it runs, the sooner it will run;
    2. Doonsubscribe () is followed by Subscribeon (), then Doonsubscribe () will be consistent with the thread specified in Subscribeon (). Suppose Doonsubscribe () after Subscribeon (), his running thread has to look at the situation analysis;
    3. Doonsubscribe () assumes that after Observeon () (note: Observeon () does not immediately call the Subcribeon () method). Then the doonsubscribe running thread is the main thread, which is not related to the thread specified by Observeon ().
    4. Assuming that the Subcribeon () method was not called before Observeon (), the Subcribeon () method is called before the Subscribe polygon () method is Observeon (), Then he will change the thread in which all the Doonsubscribe () methods are called in the entire code FLOW. The same time the Observeon () method is also changed before the entire operator of the thread (there is an important premise: not meet the 2nd condition, that is, Doonsubscribe () did not call the Subscribeon () method).
    5. Assuming that the Subcribeon () method has not been called before or after Observeon (), the Doonsubscribe () in the entire code flow runs on the main thread, regardless of the thread specified by Observeon (). At the same time, the operator before Observeon () will also run on the main thread, and the operator after Observeon () is consistent with the thread specified by Observeon ().

Analysis here today, if there is a problem please feedback Communication.

Doonsubscribe run threading Analysis by default in Rxjava

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.