Reactive Extensions (Rx) concurrency analysis Isun
Design & Code
. NET parallel Programming-reactive Extensions (Rx) concurrency analysis
About reactive Extensions (Rx)
for reactive Extensions (Rx), let's take a look at the official description from Microsoft:
The reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences a nd linq-style query operators. Using Rx, developers represent asynchronous data streams with observables, query asynchronous Data Strea Ms using LINQ operators, and parameterize the concurrency in the asynchronous data streams using schedulers. Simply put, Rx = observables + LINQ + schedulers.
The reactive Extensions (RX) is a class library that integrates asynchronous, observable (observable) sequence-based event-driven programming and Linq-style query operations. With RX, developers can use observable objects to describe asynchronous data flows, use LINQ operators to query data asynchronously, and use schedulers to control concurrency in asynchronous processes. In short, Rx = observables + LINQ + schedulers. ”
Reactive Extensions (RX) must be multithreaded?
In the above description, there is a recurring word "async". In general, the mention of "async", the first response is multithreading. The question comes, using reactive extensions must mean multithreading? Let's look at an example, where the code comes in:
1 static void Main (string[] args) 2 {3 Console.WriteLine ("Starting on Threadid:{0}", THREAD.CURRENTTHREAD.MANAGEDTHREADID); 4 var sub = new Subject<object> (); 5 6 Sub. Subscribe (o = Console.WriteLine ("Received {1} on threadid:{0}", //For observable subscription processor (handler), output handler thread ID 7 Thread.CurrentThread.ManagedThreadId, 8 o)); 9 Parameterizedthreadstart notify = obj = //delegate definition, which outputs the thread of the observed object Id10 {one Console.WriteLine (" OnNext ({1}) on threadid:{0} ", thread.currentthread.managedthreadid,13 obj); OnNext (obj), };16 notify (1), and new Thread (notify). Start (2); new Thread (notify). Start (3); Console.read (); 21}
In the code, the thread ID of the notifier and the thread ID of the callback handler are output separately. This is used by the RX default thread concurrency mode. The output results are as follows:
Both the notifier and the processing method are on the same thread, either at the current thread invocation or city threads execution. In this example, the thread assignment of Rx is working in free-threaded mode, and free-threaded means that we do not forcibly refer to the subscription in its RX, notification the thread of execution. This is the default operating mode for Rx, and in this mode Subscribing/call OnNext does not raise a new thread to process the observable sequence, the line is single-threaded (onethreaded Apartment, STA). So, we can say this: single-threaded is the default way to handle reactive Extensions (Rx).
Use Subscribeon to control the context of a subscription (subscribing)
The iobservable<tsource> extension method subscribeon<tsource> (IScheduler) allows us to pass in a scheduler (Scheduler) to control the context of the subscription execution.
1 static void Main (string[] args) 2 {3 Console.WriteLine ("Starting on Threadid:{0}", Thread.CurrentThread.Manag Edthreadid); 4 var Source = observable.create<int> (5 o = 6 {7 Console.WriteLine ("invoked on threadid:{0 } ", Thread.CurrentThread.ManagedThreadId); 8 O.onnext (1); 9 O.onnext (2); O.onnext (3); o.oncompleted (); Console.WriteLine ("Finished on ThreadId : {0} ", Thread.CurrentThread.ManagedThreadId); disposable.empty;15}); source17 //. Subscribeon (Scheduler.threadpool) 18. Subscribe (o = Console.WriteLine ("Received {1} on threadid:{0}", thread.currentthread.managedthreadid,21 o), () = Console.WriteLine ("OnCompleted on Threadid:{0}", Thread.CurrentThread.ManagedThreadId)); 24 Console.WriteLine ("Subscribed on Threadid:{0}", Thread.CurrentThread.ManagedThreadId); Console.read ();}
Code, create a observable sequence using observable.create, and then subscribe to the sequence. The output is:
When the sequence is subscribed to the source. Subscribe, the proxy observable.create is invoked to execute. The first is OnNext (1) handler, which in turn is OnNext (2) OnNext (3) handler and oncompleted handler, and finally executes to "subscribed on Threadid:10". The whole process is linear, blocking (block)-type. This is in accordance with the above analysis of the RX default single thread mode.
Let's go. Subscribeon (Scheduler.threadpool), specifies that the RX work is done in the thread pool. The results of the implementation are as follows:
As you can see, all the handler are done within a new thread. This is a non-blocking (no=-block) mode.
The Subscribeon method is commonly used to specify the thread execution mode for observable notifications (where execution is performed). It is commonly used in the following scenarios:
"UI thread does not allow blocking
"Does not need to update the display in the UI thread
Common Scheduler Properties:
CurrentThread |
Dispatch work as soon as possible in the current process, synchronization (Synchronous,block) |
Immediate |
Scheduling work immediately in the current process, synchronizing (Synchronous,block) |
Newthread |
Dispatching work in a new thread (Asynchronous,no-block) |
Taskpool |
Dispatching work in the Task Factory (Asynchronous,no-block) |
ThreadPool |
Scheduling work in a thread pool (asynchronous,no-block) |
Sample code Rxconcurrencysample Download
Resources:
The reactive Extensions (Rx) ...
Reactive Extensions (RX) Introduction
Category: Parallel Programming Tags: C #, parallel programming, reactive Extensions, Rx, Observable, Scheduler, multithreading
Reactive Extensions (Rx) concurrency analysis