开发者

why not to use thread.sleep for no reason, and explain it to a programmer

While passing through code in our proje开发者_Python百科ct I came across a web method that had this code at the end of it:

thread.sleep(6000);
return true;

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.

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.

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?


While delaying:

  • You are using/blocking a thread
  • You are consuming memory
  • You have an open TCP/IP connection

these are all expensive resources on server

Because:

  • If another requests comes in, the odds are greater a new thread must be created, so this will use CPU, memory etc, and this will delay this request. (Goto start of sentence).
  • More memory consumption, means more page faults, larger disk queue. All requests take longer.
  • TCP/IP connections are a limited resource.


This will end up having way to many blocked threads on the server.
Lets say you have 100 requests / second you have 600 threads sleeping.
Those threads will use 1MB RAM in stack space, you´re then wasting 600MB of server RAM.


ROFL - animation delay implemented on a server side :D

Putting a delay with an excuse "a client needs that delay" makes the method client-aware. And that's a smell. In a sense it also violates SRP - because now method does two things (does something useful AND makes a delay) and if you want to keep sleep you must indicate it so in the name, something like: DoSomethingUsefulAndDelayToo().

But for me the "method shall be caller agnostic" should be the main point.

The excuse "we need a delay" violates separation of concerns principle - since now your method not only gets you data, but also contaminated by a presentation logic (animation).

Besides, you can introduce delay into animation queue can (and should) easily be done using jQuery.

Principles are there for a reason, it is from experience that violating principles impact is not always immediately apparent in every case, but in most cases it will come back and hunt you down.

If he keeps insisting, at least break the method into two - one method will just do "sleep(6000); return;" now THAT would be ridiculous.


Because a web method could have multiple consumers, not all of them wanting their data to delayed.

Update

Ok, well there are also a finite number of worker threads in the pool that will process client requests. You don't want to tie them up doing nothing really. It's a function of the UI to delay the display of data, not the web service/page method that provides data. You wouldn't put a sleep in your data access layer, why would you put it in a WebMethod?


how can I explain to the programmer why this is so wrong?

Simple. The use of Sleep() is a code smell. Period.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜