Rxjava using (c) Scheduler line program control system

Source: Internet
Author: User

Objective

By default, RxJava follows a thread-invariant principle when a thread is not specified, that is, in which thread calls subscribe (), on which thread the event is produced, and on which thread the event is consumed.

If you need to switch threads, you need to use the Scheduler (scheduler).

Schedulers part of the main from the "to Android developers RxJava detailed"

schedulers Introduction

In RxJava, the scheduler--Scheduler, equivalent to the thread controller, RxJava through it to specify what thread each piece of code should run on. RxJava has built-in several Scheduler that 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 (): Scheduler used by I/O operations (read-write files, read-write databases, network information interactions, and so on). The behavior pattern is almost the same as Newthread (), except that the internal implementation of IO () is the use of an unlimited thread pool that can reuse idle threads, so that Io () is more efficient in most cases than Newthread (). Do not put the calculation work in IO (), you can avoid creating unnecessary threads.

Schedulers.computation (): Calculates the Scheduler used. 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 with a size of CPU cores. Do not put the I/O operation in computation (), otherwise the wait time of the I/O operation wastes the CPU.

Androidschedulers.mainthread (), a lightweight extension to RxJava in Rxandroid, provides Scheduler for the main thread of Android, and it specifies that the operation will run on the Android main thread.

Line Program Control system

Using the Observeon () and Subscribeon () methods for thread control,

Subscribeon (): Specifies the thread that occurs subscribe (), which is the thread where observable.onsubscribe is activated. Or an event-generated thread.

observeOn(): Specifies the thread that the Subscriber is running on. Or a thread called event consumption.

        observeOn() specified is the Subscriber the thread, and this Subscriber and not necessarily thesubscribe() parameter in the Subscriber(this piece of referenceRxJavaTransform section), but observeOn() when executing the current Observable The corresponding Subscriber , that is, its direct subordinate Subscriber .

in other words, observeOn() Specifies the thread on which the action after it is located. So if there is a need to switch threads multiple times, just call observeOn() It once at each location where you want to switch the thread .

 

The following example simulates a long time acquisition of data in Observable.onsubscribe's call (), displaying data to the UI in Subscriber's Nonext ().

Observable.create (New observable.onsubscribe<string> () {
@Override
public void call (SUBSCRIBER&LT;? Super String> Subscriber) {
Subscriber.onnext ("Info1");

Systemclock.sleep (2000);
Subscriber.onnext ("info2-sleep 2s");

Systemclock.sleep (3000);
Subscriber.onnext ("Info2-sleep 3s");

Systemclock.sleep (5000);
Subscriber.oncompleted ();
}
})
. Subscribeon (Schedulers.io ())//Specify Subscribe () occurs on the IO thread
. Observeon (Androidschedulers.mainthread ())//Specifies that the subscriber callback occurs in the main thread
. Subscribe (New subscriber<string> () {
@Override
public void oncompleted () {
LOG.V (TAG, "oncompleted ()");
}

@Override
public void OnError (Throwable e) {
LOG.V (TAG, "OnError () e=" + e);
}

@Override
public void OnNext (String s) {
Showinfo (s); UI View Display Data
}
});


Note: both Subscribeon () and Observeon () will return a new observable, so instead of using the direct flow method above, it is called in step, and the new returned observable need to be assigned to the original observable, Otherwise, thread scheduling will not work.


1) using the following method, it is found that "Onsubscribe" is still running in the default thread, because after subscribeon such operations, a new observable is returned.

Observable.subscribeon (Schedulers.io ());
Observable.observeon (Androidschedulers.mainthread ());
Observable. Subscribe (subscribe);

2) can be modified to the following two ways:

Observable = Observable.subscribeon (Schedulers.io ());
Observable = Observable.observeon (Androidschedulers.mainthread ());
Observable. Subscribe (subscribe);
////
Observable.subscribeon (Schedulers.io ())
. Observeon (Androidschedulers.mainthread ())
. Subscribe (subscribe);

Rxjava using (c) Scheduler line program control system

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.