它会在哪个线程中运行

它会在哪个线程中运行

本文介绍了Cold observable 的 Scheduler.CurrentThread - 它会在哪个线程中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 RX 的新手,开始了解一些概念……我在网上找不到的概念.

我在代码中使用了计时器:

Observable.Timer(TimeSpan.FromSeconds(2), schedulerProvider.CurrentThread);//其中 schedulerProvider.CurrentThread 实际上是 Scheduler.CurrentThread

我知道 Timer 是一个 Cold Observable,每个订阅者都将获得它的私人计时器,该计时器将在订阅后运行 2 秒.

我正在 thread1 中创建冷 observable 并使用 thread2 [, 订阅它>thread3thread4 等等...]

那么问题是:它将在哪个线程中运行?

请提供来源.

谢谢!

感谢您的详细回答.现在我知道我的问题有问题.作为第二个参数传递的调度程序 NOT 用于订阅者调度,但用于运行计时器.默认情况下,大多数 RX 操作符在同一线程上运行,但 Timer 是(几个)例外之一.默认情况下,计时器本身将在线程池上运行,并在其同一线程上调用 OnNext.如果您要求它使用 CurrentThread 调度程序,则计时器本身将在同一线程上运行(与大多数 Rx 运算符一样),并且订阅者将在同一线程上被调用.(虽然对于常规用例,这不是很推荐).

解决方案

为什么不做一些测试来找出答案?

这是我所做的:

Console.WriteLine(Thread.CurrentThread.ManagedThreadId);可观察的.Timer(TimeSpan.FromSeconds(2.0), Scheduler.CurrentThread).Subscribe(x => Console.WriteLine(Thread.CurrentThread.ManagedThreadId));

产生的:

1212

然后我尝试了这个:

Console.WriteLine(Thread.CurrentThread.ManagedThreadId);可观察的.Timer(TimeSpan.FromSeconds(2.0)).Subscribe(x => Console.WriteLine(Thread.CurrentThread.ManagedThreadId));

产生的:

1213
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);可观察的.Timer(TimeSpan.FromSeconds(2.0)).ObserveOn(Scheduler.CurrentThread).Subscribe(x => Console.WriteLine(Thread.CurrentThread.ManagedThreadId));

产生的:

1127

所有这些都归结为 Scheduler.CurrentThread 在评估 Scheduler.CurrentThread 参数时捕获当前线程上下文.

在第一个代码块中在创建计时器时 - 换句话说,我的控制台线程.

在最后一个块中,它在定时器触发后被捕获,因此它捕获了定时器触发的线程.

I'm new to RX and starting to understand a few concepts.. that one I didn't find on the web.

I'm using a timer in my code:

Observable.Timer(TimeSpan.FromSeconds(2), schedulerProvider.CurrentThread);
// where schedulerProvider.CurrentThread is actually Scheduler.CurrentThread

I understand that Timer is a Cold Observable, and every subscriber will get its private timer that will run for 2 seconds from when it subscribed.

I'm creating that cold observable in thread1 and subscribing to it with thread2 [,thread3, thread4 etc...]

So the question is: in which thread will it run?

Please provide a source.

Thank you!

EDIT: Thanks for the detailed answer. Now I know that my question has an issue.The scheduler that passed as second parameter is NOT for subscribers scheduling, but for running the Timer.Most of RX operators runs on the same thread by default, but Timer is one (of several) exceptional. By default, the timer itself will run on the Thread Pool, and call OnNext on its same thread. If you ask it to use CurrentThread scheduler, the timer itself will run on the same thread (like most of Rx operators) and subscribers will be called on the same thread. (this is not very recommended though for regular use cases).

解决方案

Why not do run some tests to find out?

Here's what I did:

Console.WriteLine(Thread.CurrentThread.ManagedThreadId);

Observable
    .Timer(TimeSpan.FromSeconds(2.0), Scheduler.CurrentThread)
    .Subscribe(x => Console.WriteLine(Thread.CurrentThread.ManagedThreadId));

The produced:

12
12

Then I tried this:

Console.WriteLine(Thread.CurrentThread.ManagedThreadId);

Observable
    .Timer(TimeSpan.FromSeconds(2.0))
    .Subscribe(x => Console.WriteLine(Thread.CurrentThread.ManagedThreadId));

That produced:

12
13
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);

Observable
    .Timer(TimeSpan.FromSeconds(2.0))
    .ObserveOn(Scheduler.CurrentThread)
    .Subscribe(x => Console.WriteLine(Thread.CurrentThread.ManagedThreadId));

That produced:

11
27

All of this boils down to Scheduler.CurrentThread capturing the current thread context at the time that the Scheduler.CurrentThread parameter is evaluated.

In the first block of code it was captured at the time that the timer was created - in other words, my console thread.

In the final block it was captured after the timer fired so it captured the thread that the timer fired on.

这篇关于Cold observable 的 Scheduler.CurrentThread - 它会在哪个线程中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:49