网站开发中文改成英文,百度浏览器官网下载并安装,湖南宏泰美佳建设工程有限公司网站,wordpress菜单子页面文章目录 Q1 无缓冲的 channel 和 有缓冲的 channel 的区别#xff1f;Q2 什么是协程泄露(Goroutine Leak)#xff1f;Q3 Go 可以限制运行时操作系统线程的数量吗#xff1f; Q1 无缓冲的 channel 和 有缓冲的 channel 的区别#xff1f;
对于无缓冲的 channel#xff0c… 文章目录 Q1 无缓冲的 channel 和 有缓冲的 channel 的区别Q2 什么是协程泄露(Goroutine Leak)Q3 Go 可以限制运行时操作系统线程的数量吗 Q1 无缓冲的 channel 和 有缓冲的 channel 的区别
对于无缓冲的 channel发送方将阻塞该信道直到接收方从该信道接收到数据为止而接收方也将阻塞该信道直到发送方将数据发送到该信道中为止。
对于有缓存的 channel发送方在没有空插槽缓冲区使用完的情况下阻塞而接收方在信道为空的情况下阻塞。
例如:
func main() {st : time.Now()ch : make(chan bool)go func () {time.Sleep(time.Second * 2)-ch}()ch - true // 无缓冲发送方阻塞直到接收方接收到数据。fmt.Printf(cost %.1f s\n, time.Now().Sub(st).Seconds())time.Sleep(time.Second * 5)
}func main() {st : time.Now()ch : make(chan bool, 2)go func () {time.Sleep(time.Second * 2)-ch}()ch - truech - true // 缓冲区为 2发送方不阻塞继续往下执行fmt.Printf(cost %.1f s\n, time.Now().Sub(st).Seconds()) // cost 0.0 sch - true // 缓冲区使用完发送方阻塞2s 后接收方接收到数据释放一个插槽继续往下执行fmt.Printf(cost %.1f s\n, time.Now().Sub(st).Seconds()) // cost 2.0 stime.Sleep(time.Second * 5)
}Q2 什么是协程泄露(Goroutine Leak)
协程泄露是指协程创建后长时间得不到释放并且还在不断地创建新的协程最终导致内存耗尽程序崩溃。常见的导致协程泄露的场景有以下几种
缺少接收器导致发送阻塞
这个例子中每执行一次 query则启动1000个协程向信道 ch 发送数字 0但只接收了一次导致 999 个协程被阻塞不能退出。
func query() int {ch : make(chan int)for i : 0; i 1000; i {go func() { ch - 0 }()}return -ch
}func main() {for i : 0; i 4; i {query()fmt.Printf(goroutines: %d\n, runtime.NumGoroutine())}
}
// goroutines: 1001
// goroutines: 2000
// goroutines: 2999
// goroutines: 3998缺少发送器导致接收阻塞 那同样的如果启动 1000 个协程接收信道的信息但信道并不会发送那么多次的信息也会导致接收协程被阻塞不能退出。 死锁(dead lock) 两个或两个以上的协程在执行过程中由于竞争资源或者由于彼此通信而造成阻塞这种情况下也会导致协程被阻塞不能退出。 无限循环(infinite loops) 这个例子中为了避免网络等问题采用了无限重试的方式发送 HTTP 请求直到获取到数据。那如果 HTTP 服务宕机永远不可达导致协程不能退出发生泄漏。
func request(url string, wg *sync.WaitGroup) {i : 0for {if _, err : http.Get(url); err nil {// write to dbbreak}iif i 3 {break}time.Sleep(time.Second)}wg.Done()
}func main() {var wg sync.WaitGroupfor i : 0; i 1000; i {wg.Add(1)go request(fmt.Sprintf(https://127.0.0.1:8080/%d, i), wg)}wg.Wait()
}Q3 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. 可以使用环境变量 GOMAXPROCS 或 runtime.GOMAXPROCS(num int) 设置例如
runtime.GOMAXPROCS(1) // 限制同时执行Go代码的操作系统线程数为 1从官方文档的解释可以看到GOMAXPROCS 限制的是同时执行用户态 Go 代码的操作系统线程的数量但是对于被系统调用阻塞的线程数量是没有限制的。GOMAXPROCS 的默认值等于 CPU 的逻辑核数同一时间一个核只能绑定一个线程然后运行被调度的协程。因此对于 CPU 密集型的任务若该值过大例如设置为 CPU 逻辑核数的 2 倍会增加线程切换的开销降低性能。对于 I/O 密集型应用适当地调大该值可以提高 I/O 吞吐率。