Reactive Extensions Observable的工作原理是什么

时间:2018-05-21 14:50:12

标签: c# multithreading system.reactive

考虑以下我从https://msdn.microsoft.com/en-us/library/hh242977(v=vs.103).aspx

获得的示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;

namespace SimpleSequence {

    class Program {

        static void RunCold() {
            IObservable<long> source = Observable.Interval(TimeSpan.FromSeconds(1));

            IDisposable subscription1 = source.Subscribe(
                x => Console.WriteLine("Observer 1: OnNext: {0}", x),
                ex => Console.WriteLine("Observer 1: OnError: {0}", ex.Message),
                () => Console.WriteLine("Observer 1: OnCompleted"));

            Thread.Sleep(TimeSpan.FromSeconds(5));

            IDisposable subscription2 = source.Subscribe(
                x => Console.WriteLine("Observer 2: OnNext: {0}", x),
                ex => Console.WriteLine("Observer 2: OnError: {0}", ex.Message),
                () => Console.WriteLine("Observer 2: OnCompleted"));

            Console.WriteLine("Press any key to unsubscribe");
            Console.ReadLine();
            subscription1.Dispose();
            subscription2.Dispose();
        }

        static void RunHot() {
            Console.WriteLine("Current Time: " + DateTime.Now);
            var source = Observable.Interval(TimeSpan.FromSeconds(1));            //creates a sequence

            IConnectableObservable<long> hot = Observable.Publish<long>(source);  // convert the sequence into a hot sequence

            IDisposable subscription1 = hot.Subscribe(                            // no value is pushed to 1st subscription at this point
                x => Console.WriteLine("Observer 1: OnNext: {0}", x),
                ex => Console.WriteLine("Observer 1: OnError: {0}", ex.Message),
                () => Console.WriteLine("Observer 1: OnCompleted"));
            Console.WriteLine("Current Time after 1st subscription: " + DateTime.Now);

            Thread.Sleep(5000);                                                   // idle for 5 seconds
            hot.Connect();                                                        // hot is connected to source and starts pushing value to subscribers 

            Console.WriteLine("Current Time after Connect: " + DateTime.Now);

            Thread.Sleep(3000);                                                   //idle for 3 seconds

            Console.WriteLine("Current Time just before 2nd subscription: " + DateTime.Now);

            IDisposable subscription2 = hot.Subscribe(                            // value will immediately be pushed to 2nd subscription
                x => Console.WriteLine("Observer 2: OnNext: {0}", x),
                ex => Console.WriteLine("Observer 2: OnError: {0}", ex.Message),
                () => Console.WriteLine("Observer 2: OnCompleted"));
            Console.ReadKey();
        }

        static void Main(string[] args) {
            RunCold();
            RunHot();
        }
    }
}

对于冷,什么线程实际生成数字?是否为每个订阅创建了一个新线程?数字似乎彼此独立,所以我认为每个都有自己的来源?

对于hot,每个订阅者共享相同的源,所以是否有一个线程生成数字,而主线程等待来自控制台的输入?

0 个答案:

没有答案