Task.Delay 在专用线程上有意义吗? [英] Does Task.Delay make sense on a dedicated thread?

查看:54
本文介绍了Task.Delay 在专用线程上有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在任务中使用 Thread.Sleep 是一个愚蠢的想法,因为它会阻塞线程池中的线程.但是,如果我有一个由 new Thread(new ThreadStart(Foo)) 创建的专用于某事的线程,Task.Delay 是否仍然有意义?无论如何都不会使用该线程.我也想知道 Task.Delay 会有什么确切的效果.它不会让线程进入睡眠状态,所以它会充当自旋等待吗?那会比睡觉更糟糕.不会吧?

I know that using Thread.Sleep in a task is a dumb idea, because it will block that thread in the thread pool. However, if I have a thread dedicated to something, created by new Thread(new ThreadStart(Foo)), does Task.Delay still make sense? Nothing is going to use that thread anyhow. I am also wondering what exact effect a Task.Delay will have. It won't put the thread to sleep, so will it act as a spin wait? That would be much worse then a sleep. Wouldn't it be?

推荐答案

我很难从问题中弄清楚这里的场景是什么;它似乎表明您正在分配一个线程,然后您希望它在一段时间内什么都不做.这是一件奇怪的事情,付钱让一个工人什么都不做.请澄清场景.

I am having difficulty figuring out from the question what the scenario here is; it seems to suggest that you are allocating a thread and you then wish it to do nothing for some amount of time. That's a strange thing to do, to pay for a worker to do nothing. Clarify the scenario please.

回答您的具体问题:

它不会让线程进入睡眠状态,所以它会充当自旋等待吗?

It won't put the thread to sleep, so will it act as a spin wait?

没有

那会比睡觉更糟糕.不会吧?

That would be much worse then a sleep. Wouldn't it be?

是的,这会很糟糕!它具有睡眠线程的所有缺点,但会消耗电力并沿途产生热量.您不仅要支付分配线程的开销,还会积极地浪费电力而没有任何好处.您永远想要旋转等待的唯一时间是当您等待发生的事情比线程量子要短得多的时间.旋转等待纳秒或最坏的情况,等待微秒,而不是毫秒.您想旋转等待需要几条指令的事情,而我们每秒运行数十亿条指令.

Yes, it would be terrible! It would have all the down sides of sleeping a thread but would consume power and make heat along the way. You'd not just be paying the overhead of allocating the thread, you'd be actively wasting power for no benefit. The only time you ever want to spin wait is when you are waiting for something that will happen in significantly less time than the thread quantum. Spin wait for nanoseconds or worst case, for microseconds, not for milliseconds. You want to spin wait for things that take a few instructions, and we run billions of instructions per second.

根据您的问题,我的怀疑是 (1) 您根本不了解 Task.Delay 的作用,以及 (2) 您正在不必要地分配线程.

Based on your question, my suspicion is (1) you don't understand at all what Task.Delay does, and (2) you are allocating a thread unnecessarily.

我们先说Task.Delay是做什么的.

Task.Delay 立即返回一个对象,表示延迟若干毫秒的任务.

Task.Delay immediately returns an object that represents the task of delaying for some number of milliseconds.

仅此而已.

真的.

它实际上不会延迟任何毫秒数.它返回一个代表延迟概念的任务.确保你头脑中清楚这一点.

It does not actually delay for any number of milliseconds. It returns a task that represents the concept of delaying. Make sure that is clear in your head.

如果您询问任务是否完成,它当然会告诉您该秒数是否已经过去.

If you ask the task whether it is complete or not, it will tell you whether that number of seconds have passed, of course.

如果您指定某个函数作为该任务的延续,该延续将被安排在任务完成后的某个时间点在当前上下文中运行.

If you assign some function as the continuation of that task, that continuation will be scheduled to run on the current context at some point after the task is complete.

然后await Task.Delay(whatever);做什么?

我只是说它的作用.Task.Delay 返回一个表示延迟的任务.await 检查该任务是否完成.如果是,则程序继续正常运行.如果任务未完成,则await 的位置成为任务的继续点,并且该方法返回到其调用者.然后,在任务完成后的某个时间点,将继续安排在当前上下文上执行.

I just said what it does. Task.Delay returns a task that represents a delay. await checks to see if that task is complete. If it is, then the program continues as normal. If the task is not complete then the location of the await becomes the continuation point of the task, and the method returns to its caller. The continuation is then scheduled to execute on the current context at some point in the future after the task completes.

你为什么要雇佣工人来做这件事,我不知道也不明白.雇佣工人来做 CPU 密集型的工作,他们在完成之前不会停止在 CPU 上敲打一纳秒.不要雇佣工人做一次延迟几毫秒的事情,比如访问磁盘或延迟工作!

Why you would hire a worker in order to do this, I do not know or understand. Hire workers to do jobs that are CPU intensive, where they do not stop hammering on the CPU for a nanosecond until they are done. Do not hire workers to do things that delay for milliseconds at a time, like accessing disks or delaying work!

这篇关于Task.Delay 在专用线程上有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆