7. 官方定義
RxJava is a Java VM implementation of Reactive Extensions: a library for composing
asynchronous and event-based programs by using observable sequences.
It extends the observer pattern to support sequences of data/events and adds
operators that allow you to compose sequences together declaratively while
abstracting away concerns about things like low-level threading, synchronization,
thread-safety and concurrent data structures.
7
“
”
要使用一個工具前我們需要先建立好環境
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.// (see https://github.com/ReactiveX/RxJava/releases for latest 2.x.x version)RxAndroid is specific to Android Platform with few added classes on top of RxJava.
多了執行緒
Observable: Observable is a data stream that do some work and emits data.
Observer: Observer is the counter part of Observable. It receives the data emitted by Observable.
Subscription: The bonding between Observable and Observer is called as Subscription. There can be multiple Observers subscribed to a single Observable.
衍生出來解決上述問題
Operator / Transformation: Operators modifies the data emitted by Observable before an observer receives them.
一般的操作是沒有切換執行緒的Schedulers: Schedulers decides the thread on which Observable should emit the data and on which Observer should receives the data i.e background thread, main thread etc.,
Schedulers.io() – 主要是處理非密集型的操作,像是遠端要求,像本地端讀取資料,資料庫操作等。它支援thread-pool,所以可以重複使用已經創建的執行緒,並且會移除持續一分鐘沒有工作的執行緒,但是它的執行緒創建是沒有上限的,這可能會在大量創建實兆呈效能上的問題。
AndroidSchedulers.mainThread() – 處理畫面更心上的操作,也就是android UI thread
Schedulers.newThread() – 會為你要執行的task創建一個新的執行緒,適合非常長時間的操作,而且該執行緒是不會被重複利用的。
Schedulers.computation() – 它的thread-pool的執行緒上限會依照可執行核心數去創建,適合大量的運算,像是bitmap processing。
Schedulers.single() – 讓所有task依序在同一個執行緒執行,適合有順序需求的情況。
Schedulers.trampoline() – 讓所有的task依你給予的順序(FIFO)執行於current thread,可以用於unit test。
Schedulers.from() – 由自己定義的 executor 來創建.
Schedulers.immediate() – This scheduler executes the the task immediately in synchronous way by blocking the main thread.
provides an object reference variable which can be read and written atomically.
By atomic is meant that multiple threads attempting to change the same AtomicReference (e.g. with a compare-and-swap operation) will not make the AtomicReference end up in an inconsistent state. AtomicReference even has an advanced compareAndSet() method which lets you compare the reference to an expected value (reference) and if they are equal, set a new reference inside the AtomicReference object.
/**
* Returns an instance which creates asynchronous observables. Applying
* {@link Observable#subscribeOn} has no effect on stream types created by this factory.
*/
/**
* Returns an instance which creates synchronous observables that
* {@linkplain Observable#subscribeOn(Scheduler) subscribe on} {@code scheduler} by default.
*/
"Why should I consider adopting RP?"
Reactive Programming raises the level of abstraction of your code so you can focus on the interdependence of events that define the business logic, rather than having to constantly fiddle with a large amount of implementation details. Code in RP will likely be more concise.
The benefit is more evident in modern webapps and mobile apps that are highly interactive with a multitude of UI events related to data events. 10 years ago, interaction with web pages was basically about submitting a long form to the backend and performing simple rendering to the frontend. Apps have evolved to be more real-time: modifying a single form field can automatically trigger a save to the backend, "likes" to some content can be reflected in real time to other connected users, and so forth.
Apps nowadays have an abundancy of real-time events of every kind that enable a highly interactive experience to the user. We need tools for properly dealing with that, and Reactive Programming is an answer.