2015-09-10 65 views
9

阅读this great answer有关节点的线程性质, 后,我开始与UV_THREADPOOL_SIZE系统变量发挥改变线程池的大小,我发现一些有趣的事情:Node实际创建多少个线程?

当我设置

process.env.UV_THREADPOOL_SIZE = 10; 

我得到15线程在我的节点进程(我认为它应该是10 + 1主节点线程= 11)。

看一看我的脚本:

process.env.UV_THREADPOOL_SIZE = 10; 

//init thread pool by calling `readFile` function 
require('fs').readFile(__filename, 'utf8', function(err, content) {}); 

//make node not exiting 
setInterval(function() {}, 1000); 

它运行后I型:

ps -Lef | grep test.js | grep -v grep 

,并得到下面的结果:

olegssh 4869 4301 4869 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4870 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4871 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4872 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4873 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4874 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4875 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4876 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4877 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4878 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4879 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4880 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4881 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4882 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 
olegssh 4869 4301 4883 0 15 16:38 pts/0 00:00:00 /home/olegssh/node/bin/node test.js 

正如你可以看到有运行15个线程。

如果我设置UV_THREADPOOL_SIZE = 1,我得到6个线程。

如果我注释掉readFile行(所以线程池没有初始化),我得到5个线程。

因此我得出结论,Node在启动时创建5个线程。为什么不是1?

有人可以对此有所了解吗?

编辑:我使用的是全新的节点4.0.0

回答

9

4个额外的线程是for use by V8。 V8使用这些线程来执行各种任务,例如GC相关的后台任务和优化编译器任务。

+0

简单明了。谢谢! :) – Curious