为什么Thread.sleep()方法是让CPU密集型? [英] Why Thread.Sleep() is so CPU intensive?

查看:670
本文介绍了为什么Thread.sleep()方法是让CPU密集型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个pseduo code ASP.NET页面:

I have an ASP.NET page with this pseduo code:

while (read)
{
   Response.OutputStream.Write(buffer, 0, buffer.Length);
   Response.Flush();
}

任何客户端谁要求该页面将开始下载二进制文件。一切都在这一点上确定的,但客户必须下载速度没有限制,以便改变上述code这样:

Any client who requests this page will start to download a binary file. Everything is OK at this point but clients had no limit in download speed so changed the above code to this:

while (read)
{
   Response.OutputStream.Write(buffer, 0, buffer.Length);
   Response.Flush();
   Thread.Sleep(500);
}

现在速度的问题是解决了,但在与谁接了一个又一个100个并发客户端测试(3秒钟每个新的连接之间的滞后)的CPU使用率增加当客户数量的增加,当有70〜80个并发客户端CPU达到100%和任何新的连接被拒绝。数字可能会在其他机器不同,但问题是,为什么Thread.sleep()方法是如此CPU密集型,有没有什么办法,以加快完成,无需CPU上涨客户端?

Speed problem is solved now, but under test with 100 concurrent clients who connect one after another (3 seconds lag between each new connection) the CPU usage increases when the number of clients increases and when there are 70 ~ 80 concurrent clients CPU reaches 100% and any new connection is refused. Numbers may be different on other machines but the question is why Thread.Sleep() is so CPU intensive and is there any way to speed done the client without CPU rising ?

我可以在IIS级别做到这一点,但我需要从我的应用程序内部更多的控制。

I can do it at IIS level but I need more control from inside of my application.

推荐答案

只是一个猜测:

我不认为这是 Thread.sleep代码()就是占用CPU的 - 那就是你造成线程事实捆绑起来响应请求这么久了,并且系统需要旋转起来新主题(和其他资源),以新的请求,因为这些睡眠的线程将不再在线程池中的可用回应。

I don't think it's Thread.Sleep() that's tying up the CPU - it's the fact that you're causing threads to be tied up responding to a request for so long, and the system needs to spin up new threads (and other resources) to respond to new requests since those sleeping threads are no longer available in the thread pool.

这篇关于为什么Thread.sleep()方法是让CPU密集型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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