Thread.Sleep(1) 的目的? [英] Purpose of Thread.Sleep(1)?

查看:53
本文介绍了Thread.Sleep(1) 的目的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一些线程基础知识,并在 msdn 网站上找到了这段代码.

I was reading over some threading basics and on the msdn website I found this snippet of code.

    // Put the main thread to sleep for 1 millisecond to
    // allow the worker thread to do some work:
    Thread.Sleep(1);

这是该页面的链接:http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.80).aspx.

为什么主线程会休眠1毫秒?如果主线程持续运行,辅助线程是否不会启动其任务?还是该示例适用于需要 1 毫秒完成的任务?好像任务一般需要5秒才能完成,主线程应该休眠5000毫秒?

Why does the main thread have sleep for 1 millisecond? Will the secondary thread not start its tasks if the main thread is continuously running? Or is the example meant for a task that takes 1 millisecond to do? As in if the task generally takes 5 seconds to complete the main thread should sleep for 5000 milliseconds?

如果这仅仅是关于 CPU 使用率,这里有一个类似的 关于Thread.Sleep的问题.

If this is solely regarding CPU usage, here is a similar Question about Thread.Sleep.

如有任何意见,我们将不胜感激.

Any comments would be appreciated.

谢谢.

推荐答案

该代码中的 1 并不是很特别;它总是最终睡得比这更长,因为事情不是那么精确,而且放弃你的时间片并不等于操作系统保证你什么时候能取回它.

The 1 in that code is not terribly special; it will always end up sleeping longer than that, as things aren't so precise, and giving up your time slice does not equal any guarantee from the OS when you will get it back.

Thread.Sleep() 中的时间参数的目的是让您的线程在至少这段时间内让步,大致如此.

The purpose of the time parameter in Thread.Sleep() is that your thread will yield for at least that amount of time, roughly.

所以该代码只是明确地放弃了它的时间段.一般来说,这样一点代码是不需要的,因为操作系统会为你管理你的线程,抢先中断它们在其他线程上工作.

So that code is just explicitly giving up its time slot. Generally speaking, such a bit of code should not be needed, as the OS will manage your threads for you, preemptively interrupting them to work on other threads.

这种代码经常用在线程示例"中,作者想要强制一些人为的出现来证明一些竞争条件等(在你的例子中似乎就是这种情况)

This kind of code is often used in "threading examples", where the writer wants to force some artificial occurrence to prove some race condition, or the like (that appears to be the case in your example)

正如 Jon Hanna 对同一问题的回答所述,Sleep(0)Sleep(1)(或任何其他非-零数字),正如 ChrisF 所暗示的,这在某些线程情况下可能很重要.

As noted in Jon Hanna's answer to this same question, there is a subtle but important difference between Sleep(0) and Sleep(1) (or any other non-zero number), and as ChrisF alludes to, this can be important in some threading situations.

两者都涉及线程优先级;线程可以被赋予更高/更低的优先级,这样只要有更高优先级的线程有任何工作要做,低优先级的线程将永远执行.在这种情况下,可能需要 Sleep(1)...但是...

Both of those involve thread priorities; Threads can be given higher/lower priorities, such that lower priority threads will never execute as long as there are higher priority threads that have any work to do. In such a case, Sleep(1) can be required... However...

低优先级线程也受制于同一系统上其他进程正在执行的操作;因此,虽然您的进程可能没有运行更高优先级的线程,但如果有其他线程运行,您的进程仍然不会运行.

Low-priority threads are also subject to what other processes are doing on the same system; so while your process might have no higher-priority threads running, if any others do, yours still won't run.

不过,这通常不是您需要担心的;默认优先级是正常"优先级,在大多数情况下,您不应更改它.提高或降低它有很多含义.

This isn't usually something you ever need to worry about, though; the default priority is the 'normal' priority, and under most circumstances, you should not change it. Raising or lowering it has numerous implications.

这篇关于Thread.Sleep(1) 的目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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