Rxjava Study (ii)

Source: Internet
Author: User

1) Scheduler API (i)

In RxJava, the Scheduler -Scheduler, the equivalent of a thread controller, RxJava it to specify what thread each piece of code should run on. RxJava has several built-in Scheduler , which are already suitable for most usage scenarios:

    • Schedulers.immediate(): Runs directly on the current thread, which is equivalent to not specifying a thread. This is the default Scheduler .
    • Schedulers.newThread(): Always enable new threads and perform operations on new threads.
    • Schedulers.io(): I/O operations (read-write files, read-write databases, network information interactions, etc.) are used Scheduler . The behavior pattern is newThread() almost the same as io() the internal implementation is the use of an unlimited number of thread pools, can reuse idle threads, so in most cases io() newThread() more efficient. Do not put the calculation work in io() , you can avoid creating unnecessary threads.
    • Schedulers.computation(): The calculation is used Scheduler . This calculation refers to CPU-intensive computations, i.e., operations that do not restrict performance by operations such as I/O, such as the calculation of a sample. This Scheduler uses a fixed thread pool, the size of which is the number of CPU cores. Do not put I/O operations in computation() , or the wait time for I/O operations wastes the CPU.
    • In addition, Android has a dedicated AndroidSchedulers.mainThread() , and it specifies that the operation will run on the Android main thread.
    • Schedulers.from (Executor): uses the specified executor as the scheduler;
    • Schedulers.test (): For testing purposes, supports advanced events for unit testing;
    • Schedulers.trampoline (): Queues the work in the current thread into the queue, and then in turn.

With these Scheduler , you can use subscribeOn() and observeOn() two methods to control the threads.

    • subscribeOn(): Specifies the thread subscribe() that occurs, that is, the Observable.OnSubscribe thread that is active. Or an event-generated thread.
    • observeOn(): Specifies the Subscriber thread that is running. Or a thread called event consumption. That is, the callback thread.

The main difference between the two is that the scope of the impact is different, Observeon is more limited, but it can be called multiple times, changing the schedule of different recipients, after the call to the function of the observable caused the impact. And Subscribeon is a one-time, regardless of where the call, always from the change of the most primitive observable began to affect the entire observable processing .

On the code:

 observable.just (1, 2, 3, 4). Subscribeon (Schedulers.io ())  //  specifies that subscribe () occurs on an IO thread . Observeon (Androidschedulers.mainthread ()) Span style= "color: #008000;" >//  Specifies that the callback for the subscriber occurs on the main thread . Subscribe ( New  Action1<integer> () {@Override  public   call (Integer number) {L        OG.D (tag,  "number:" + number); }    });

The above code, because subscribeOn(Schedulers.io()) of the specified, the content of the created event, 1 , 2 3 will be 4 emitted in the IO thread, and because observeOn(AndroidScheculers.mainThread() of the specified, so the subscriber number of printing will occur in the main thread. In fact, this subscribe() is very common in the previous writing of two sentences subscribeOn(Scheduler.io()) observeOn(AndroidSchedulers.mainThread()) , and it applies to most of the "background thread fetching data, main thread display" program policy.

And the example mentioned earlier by the picture ID gets the picture and displays, if also add these two sentences:

IntDrawableres= ...;ImageViewImageView= ...;Observable.Create(New Onsubscribe<Drawable> () { @Override Public voidPager(Subscriber<? Super Drawable>Subscriber) { DrawableDrawable=Gettheme().Getdrawable(Drawableres));Subscriber.OnNext(Drawable);Subscriber.OnCompleted(); }}).Subscribeon(Schedulers.Io()) Specifies that subscribe () occurs in the IO thread.Observeon(Androidschedulers.Mainthread()) Specifies that the callback for subscriber occurs in the main thread.Subscribe(New Observer<Drawable> () { @Override Public voidOnNext(DrawableDrawable) {ImageView.Setimagedrawable(Drawable); } @Override Public voidOnCompleted() { }  @Override  public< Span class= "PLN" > void Onerror (throwable< Span class= "PLN" > E)  {  Toast. Maketext (activity,  " error! " , toast.show (); }});           

Then, loading the picture will occur on the IO thread, and the settings picture is set to the main thread. This means that even if loading the picture takes dozens of or even hundreds of milliseconds, it does not cause the slightest lag in the interface.

Rxjava Study (ii)

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.