为什么不无缘无故地使用thread.sleep并向程序员解释 [英] why not to use thread.sleep for no reason, and explain it to a programmer

查看:64
本文介绍了为什么不无缘无故地使用thread.sleep并向程序员解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中传递代码时,我遇到了一个Web方法,该方法的末尾带有以下代码:

While passing through code in our project I came across a web method that had this code at the end of it:

thread.sleep(6000);
return true;

现在,这样做是为了延迟来自客户端的jQuery ajax调用,并且ajax动画将显示更长的时间.

Now, this was done so the jQuery ajax call from the client gets delayed and the ajax animation will show for a little bit longer.

在我看来这是非常错误的.UI和服务器端之间不应存在这种连接.如果他希望动画花费更长的时间,可以在客户端使用 setTimeOut 函数.

This is very wrong in my eyes. There shouldn't be this kind of connection between UI and server side. If he wants the animation to take longer he can use the setTimeOut function in the client side.

这是我的问题:我如何向程序员解释为什么这是错误的?不仅因为客户端/服务器问题,还为什么要在网站上调用 thread.sleep ?

Here is my problem: how can I explain to the programmer why this is so wrong? Not just because the client/server thing, but why ever call thread.sleep on a website?

推荐答案

同时延迟:

  • 您正在使用/阻止线程
  • 您正在消耗内存
  • 您具有打开的TCP/IP连接

这些都是服务器上昂贵的资源

these are all expensive resources on server

因为:

  • 如果出现其他请求,则必须创建一个新线程的可能性更大,因此将使用CPU,内存等,这将延迟此请求.(转到句子的开头).
  • 更多的内存消耗,意味着更多的页面错误,更大的磁盘队列.所有请求都需要更长的时间.
  • TCP/IP连接是有限的资源.

这篇关于为什么不无缘无故地使用thread.sleep并向程序员解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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