it really is a single-threaded mode node

     Mentioned node, we can think of single-threaded, asynchronous IO, event-driven and other words immediately. We must first clear the node is really a single-threaded, and if single-threaded, so asynchronous IO, and timed events (setTimeout, setInterval, etc.) it is to be carried out where it is.

     In fact, according strictly speaking, node is not a single-threaded. node there are a variety of threads, including:

  • Js engine thread of execution
  • Timer thread (setTimeout, setInterval)
  • Http asynchronous thread (ajax)

     ...

     We usually refer to a single thread is only one node js engine to run on the main thread. Other event-driven asynchronous IO and the related thread to achieve internal thread pool and thread scheduling by libuv. libv present in an Event Loop, switched by Event Loop multithreading to achieve similar effects. Event Loop simple terms is to maintain a stack and execute an event queue, stack if asynchronous IO and timer functions such as discovery, will put these into asynchronous callbacks to the event queue in the current execution. After the completion of the current execution stack performs, from the event queue, the event queue asynchronous callback function in a certain order.

 

 

      From the execution stack above figure, the event queue, the last event to the callback queue in a certain order, the entire process is a simplified version of the Event Loop. Also, when the callback function is executed, it will generate an execution stack, there is also the callback function may be nested asynchronous function, that is to say there are nested execution stack.

      That node in single-threaded means js engine runs only on a single main thread, other asynchronous operation, also has a separate thread to perform, to achieve a similar multi-thread context switching and thread through the Event Loop libv pool scheduling. A thread is the smallest of the process, and therefore node is a single process. This explains why the node is single-threaded and single-process.

Guess you like

Origin www.cnblogs.com/xinsir/p/11883667.html