# Go 运行程序中的线程数

The GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously. There is no limit to the number of threads that can be blocked in system calls on behalf of Go code; those do not count against the GOMAXPROCS limit. This package's GOMAXPROCS function queries and changes the limit.

## 什么是blocking的系统调用?

A blocking system call is one that must wait until the action can be completed. read() would be a good example - if no input is ready, it'll sit there and wait until some is (provided you haven't set it to non-blocking, of course, in which case it wouldn't be a blocking system call). Obviously, while one thread is waiting on a blocking system call, another thread can be off doing something else.

## 线程数暴涨的简单测试

Go提供了两种查询域名的方式，CGO方式或者纯Go方式，比如net库中的DialLookupHostLookupAddr这些函数都会间接或者直接的与域名程序相关，比如上面的例子中使用LookupHost，采用不同的方式并发情况下产生的线程会不同。

## 无限暴涨？不可能!

Go运行时不会回收线程，而是会在需要的时候重用它们。但是你如果创建大量的线程，根本就是不需要的，理论上值保留一小部分线程重用就可以了。