Students who don't know Rxjava can look first.
RxJava and Rxandroid One (basic)
RxJava and Rxandroid II (use of Operators)
RxJava and Rxandroid III (Life cycle control and memory optimization)
RxJava and Rxandroid Four (use of rxbinding)
This article will have several examples to illustrate the correct use posture for Rxjava thread scheduling.
Example 1
Observable. Create (new observable.onsubscribe<string> () {@Override public void call (SUBSCRIBER<? Super String> Subscriber) {logger.v ("Rx_call", Thread. CurrentThread (). GetName ()); Subscriber.onnext ("DD"); Subscriber.oncompleted (); }}). map (new func1<string, String > () {@Override Public String Call (string s) {logger.v ("Rx_map", Thread.CurrentThread (). GetName ()); return s + "88"; }}). Subscribe (New action1<string> () {@Override public void Call (String s) {logger.v ("Rx_subscribe", Thread.CurrentThread (). GetName ()); } }) ;
Results
/rx_call:main--Main thread
/rx_map:main--Main thread
/rx_subscribe:main--Main thread
Example 2
New Thread (New Runnable () {@Override public void run () {LOGGER.V ("Rx_newthread") , Thread.CurrentThread (). GetName ()); Rx (); }}). Start (); void Rx () {Observable. Create (new observable.onsubscribe<string> () {@Overr IDE public void call (SUBSCRIBER<? Super String> Subscriber) {LOGGER.V ("R X_call ", Thread.CurrentThread (). GetName ()); Subscriber.onnext ("DD"); Subscriber.oncompleted (); }}). map (new func1<string, String > () {@Override Public String Call (string s) {logger.v ("Rx_map", Thread.CurrentThread (). GetName ()); return s + "88"; }}). Subscribe (New action1<string> () { @Override public void Call (String s) {logger.v ("Rx_subscribe", Thread.CurrentThread (). GetName ()); } }) ; }
Results
/rx_newthread:thread-564--Child thread
/rx_call:thread-564--Child thread
/rx_map:thread-564--Child thread
/rx_subscribe:thread-564--Child thread
- By example 1 and Example 2, it is stated that Rxjava is run by default in the current thread. If the current thread is a child thread, Rxjava runs on the child thread, and similarly, the current thread is the thread, then Rxjava runs on the main thread
Example 3
Observable. Create (new observable.onsubscribe<string> () {@Override public void call (SUBSCRIBER<? Super String> Subscriber) {logger.v ("Rx_call", Thread. CurrentThread (). GetName ()); Subscriber.onnext ("DD"); Subscriber.oncompleted (); }}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()) . Map (new func1<string, String > () {@Override public String CA LL (String s) {logger.v ("Rx_map", Thread.CurrentThread (). GetName ()); return s + "88"; }}). Subscribe (New action1<string> () {@Override public void Call (String s) {logger.v ("Rx_subscribe", Thread.CurrentThread (). GetName ()); } }) ;
Results
/rx_call:rxcachedthreadscheduler-1--io Thread
/rx_map:main--Main thread
/rx_subscribe:main--Main thread
Example 4
Observable. Create (new observable.onsubscribe<string> () {@Override public void call (SUBSCRIBER<? Super String> Subscriber) {logger.v ("Rx_call", Thread. CurrentThread (). GetName ()); Subscriber.onnext ("DD"); Subscriber.oncompleted (); }}). map (new func1<string, String > () {@Override Public String Call (string s) {logger.v ("Rx_map", Thread.CurrentThread (). GetName ()); return s + "88"; }}). Subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()) . Subscribe (New action1<string> () {@Override public void call ( String s) {logger.v ("Rx_subscribe", Thread.CurrentThread (). GetName ()); } }) ;
Results
/rx_call:rxcachedthreadscheduler-1--io Thread
/rx_map:rxcachedthreadscheduler-1--io Thread
/rx_subscribe:main--Main thread
- By Example 3, example 4 can be seen. Subscribeon (Schedulers.io ()) and. Observeon (Androidschedulers.mainthread ()) write a different position, resulting in a different result. As you can see from Example 4, the map () operator runs by default in the thread that generated the event. event Consumption is only in subscribe ().
- For Create (), just (), from ()---events
map (), Flapmap (), scan (), filter (), etc.--event processing
Subscribe ()--Event consumption
- Event Generation: Run by default on the current thread, can be custom thread by Subscribeon ()
Event Processing: The default is consistent with the thread generated by the event and can be customized by the Observeon () thread
event consumption: run by default on the current thread, can have Observeon () custom
Example 5 switching threads multiple times
Observable. Create (new observable.onsubscribe<string> () {@Override public void call (SUBSCRIBER<? Super String> Subscriber) {logger.v ("Rx_call", THREAD.C Urrentthread (). GetName ()); Subscriber.onnext ("DD"); Subscriber.oncompleted (); }}). Observeon (Schedulers.newthread ())//new thread. Map (func1<string, String > () {@Override public String call (string s) {Logg ER.V ("Rx_map", Thread.CurrentThread (). GetName ()); return s + "88"; }}). Observeon (Schedulers.io ())//io thread. Filter (new func1<string, B Oolean> () {@Override public Boolean call (String s) {Logg ER.V ("Rx_filter ", Thread.CurrentThread (). GetName ()); return s! = NULL; }}). Subscribeon (Schedulers.io ())//define Event Generation thread: IO thread. Observeon (Androidsche Dulers.mainthread ())///Event consumption thread: main thread. Subscribe (new action1<string> () {@Override public void Call (String s) {logger.v ("Rx_subscribe", Thread.CurrentThread (). GetName ()); } }) ;
Results
/rx_call:rxcachedthreadscheduler-1--IO thread
/rx_map:rxnewthreadscheduler-1--New out of the thread
/rx_filter:rxcachedthreadscheduler-2--IO thread
/rx_subscribe:main--Main thread
RxJava and rxandroid Five (thread scheduling)