node.js服务器如何优于基于线程的服务器

问题描述:

Node.js服务器适用于支持回调函数的基于事件的模型。但我无法理解它比线程等待系统IO的传统线程服务器更好。在基于线程的模型的情况下,当线程需要等待IO时,它被抢占,因此不消耗CPU周期因此不会导致等待时间。

Node.js server is works on event based models where callback functions are supported. But I am not able to understand how is it better than traditional thread based servers where threads wait for system IO. In case of thread based model, when a thread needs to wait for IO, it gets preempted so doesn't consume CPU cycles hence doesn't contribute to wait time.

Node.js如何改善等待时间?

How Node.js improves wait time?

线程是相对较重的对象,其资源占用空间一直延伸到内核中。当您将一个线程停放在阻塞系统调用或互斥锁或条件变量中时,您将占用所有这些资源,但什么都不做。现在操作系统必须找到更多的资源,这样你的程序就可以创建另一个线程......然后你也将它们闲置了。不久之后操作系统就会努力为你的程序寻找更多的资源来浪费。

Threads are relatively heavy-weight objects that have a resource footprint extending all the way into the kernel. When you park a thread in a blocking syscall or on a mutex or condition variable, you are tying up all those resources but doing nothing. Now the OS has to find more resources so your program can create another thread... Then you idle them too. It doesn't take long before the OS is struggling to scavenge more resources for your program to waste.

CPU时间只是他大局的一小部分。 : - )

CPU time is just one small part of he bigger picture. :-)