解决Golang中goroutine执行速度的问题
突然想到了之前一直没留意的for循环中开goroutine的执行顺序问题,就找了段代码试了试,试了几次后发现几个有意思的地方,我暂时没有精力往更深处挖掘,希望有golang大神能简单说一说这几个地方是怎么回事。
代码:
package main import "fmt" func count(ch chan int) { fmt.println("count doing") ch <- 1 fmt.println("counting") } func main() { chs := make([]chan int, 100) for i := 0; i < 100; i++ { chs[i] = make(chan int) go count(chs[i]) fmt.println("count",i) } for i, ch := range chs { <-ch fmt.println("counting ", i) } }
试了几次之后,反复的想goroutine执行的问题。
根据下面的输出,我能看到的是:
1. for循环的速度 比 for中开出goroutine并执行的速度 执行的快
2. 但是 开goroutine和执行第一个fmt的速度可能赶上 for循环的速度 比如前12个count和count doing
3. 关键问题,第二个for循环执行的fmt竟然要比goroutine中的第二个fmt快??(放入channel很耗时?)
4. main结束时,也就是第二个for循环结束时, 还有goroutine中的第二个fmt没执行
输出:
count 0 count 1 count 2 count 3 count 4 count 5 count 6 count 7 count 8 count 9 count 10 count 11 count doing count doing count doing count doing count doing count 12 count doing count doing count doing count doing count doing count doing count doing count 13 count 14 count 15 count 16 count 17 count 18 count 19 count 20 count 21 count doing count doing count doing count 22 count doing count doing count doing count 23 count 24 count 25 count 26 count 27 count 28 count 29 count 30 count doing count 31 count doing count doing count 32 count 33 count 34 count 35 count doing count 36 count doing count doing count 37 count 38 count doing count doing count doing count doing count 39 count 40 count 41 count 42 count 43 count doing count doing count 44 count 45 count 46 count 47 count doing count 48 count 49 count doing count doing count 50 count 51 count doing count doing count doing count doing count doing count 52 count 53 count doing count doing count doing count doing count 54 count doing count 55 count 56 count 57 count 58 count 59 count 60 count 61 count 62 count 63 count 64 count 65 count doing count doing count doing count 66 count 67 count 68 count 69 count doing count 70 count doing count 71 count 72 count doing count 73 count doing count doing count 74 count doing count 75 count 76 count 77 count doing count doing count doing count doing count 78 count 79 count 80 count 81 count 82 count 83 count 84 count 85 count 86 count 87 count 88 count 89 count 90 count 91 count 92 count 93 count 94 count doing count doing count doing count doing count doing count doing count doing count doing count 95 count doing count 96 count doing count 97 count 98 count doing count 99 count doing count doing counting 0 counting 1 counting 2 counting 3 counting 4 counting 5 counting 6 count doing count doing counting 7 counting 8 count doing counting count doing counting 9 counting count doing count doing count doing count doing count doing counting count doing count doing count doing counting count doing counting count doing counting 10 counting 11 counting count doing count doing count doing count doing count doing count doing counting count doing count doing counting counting count doing count doing count doing count doing counting count doing counting count doing count doing counting 12 counting 13 counting 14 counting 15 counting 16 counting 17 counting 18 counting 19 counting 20 counting 21 counting 22 counting 23 counting 24 counting 25 counting 26 counting 27 counting 28 counting 29 counting 30 counting 31 counting 32 counting 33 counting 34 counting 35 counting 36 counting 37 counting 38 counting 39 counting 40 counting 41 counting 42 counting 43 counting 44 counting 45 counting 46 counting 47 counting 48 counting 49 counting 50 counting 51 counting 52 counting 53 counting 54 counting 55 counting 56 counting counting counting counting counting counting count doing counting count doing counting counting counting 57 counting 58 counting 59 counting 60 counting 61 counting 62 counting 63 counting 64 counting 65 counting 66 counting 67 counting 68 counting 69 counting 70 counting 71 counting 72 counting 73 counting 74 counting 75 counting 76 counting counting counting counting counting counting counting counting counting counting counting counting counting counting counting counting counting counting counting counting 77 counting 78 counting 79 counting 80 counting 81 counting 82 counting 83 counting 84 counting 85 counting 86 counting 87 counting 88 counting 89 counting 90 counting 91 counting 92 counting 93 counting 94 counting 95 counting 96 counting 97 counting 98 counting 99
补充:【golang】goroutine调度的坑
今天说说我遇到的一个小坑, 关于goroutine 调度的问题。
关于goroutine的调度,网上资料已经一大堆了,这里就不再赘述了。
还是简单的说一下我理解的goroutine的调度。goroutine是语言层面的,它和内核线程是m:n的关系,并且用了分段栈,是相当轻量了。
如果设置runtime.gomaxprocs为1,那么会有一个上下文g,在g上会有一个对应的内核线程m,内核线程m上可以对应很多个goroutine记作g,每个上下文都会有一个队列称作runqueue,在用go关键字开启一个goroutine的时候,该goroutine就会被装入runqueue中,然后被m用来执行,如果刚好有两个goroutine在队列里,先执行的goroutine因为执行一些耗时操作(系统调用,读写 channel,gosched 主动放弃,网络io)会被挂起(扔到全局runqueue),然后调度后面的goroutine。
好,重点在这里,看一下下面的一段代码
func main(){ runtime.gomaxprocs(1) waitgroup.add(1) go func(){ defer waitgroup.done() for i := 0;i < 20;i++ { fmt.println("hello") f, _ := os.open("./data") f.write([]byte("hello")) } }() waitgroup.add(1) go func(){ defer waitgroup.done() for { } }() waitgroup.wait() }
这段代码你运行,你会发现,永远都会被阻塞住,hello永远都打印不出来
好,这里出现了两个问题
1.为什么死循环的goroutine总是先运行?按理说不应该是随机的吗?
2.为什么死循环的goroutine会阻塞而没有被挂起?
先看第二个问题。这里的话,我当时也很苦恼,于是在网上发了问题,得到的回复是,死循环不属于上述任何一种需要被挂起的状态,于是死循环的goroutine会一直运行,想象一个高并发的场景,如果其中一个goroutine因为某种原因陷入死循环了,当前执行这个goroutine的os thread基本就会被一直执行这个goroutine,直到程序结束,这简直是一场灾难。但是,1.12 会修正这个小问题。我们还是默默的等待新版本发布吧。
再看第一个问题。为什么死循环的goroutine总是先运行?按理说不应该是随机的吗?测试过很多次,都是第二个goroutine先运行。嗯,其实就算是第二个goroutine先运行也是具有随机性的,这关于golang的编译器如何去实现随机。看一下大佬的回答 :
<不是说测试很多遍它就会一直这样,语言规范没有说必须是这个顺序,那编译器怎么实现都可以,因为都不违反规范。
所以你要把它看作是随机的,不能依赖这种未确定的行为,不然很可能新版的编译器就会破坏你依赖的事实。有些项目不敢升级编译器版本,就是因为依赖了特定版本的编译器的行为,一升级就坏了。
不是你自己测试很多遍你就能依赖它,编译器、操作系统、硬件等等不同,都有可能出现不同的结果。可以依赖的只有语言规范( https://golang.org/ref/spec ),编译器实现者是一定会遵守的。
到这里也算是解决了上述的两个问题了。
来看一下另外一个版本
func main(){ runtime.gomaxprocs(1) waitgroup.add(1) go func(){ defer waitgroup.done() for { } }() waitgroup.add(1) go func(){ defer waitgroup.done() for i := 0;i < 20;i++ { fmt.println("hello") f, _ := os.open("./data") f.write([]byte("hello")) http.get("http://www.baidu.com") fmt.println("request successful") } }() waitgroup.wait() }
执行结果是,会先打印一个hello,然后陷入死循环,这也是说明了goroutine在遇到耗时操作或者系统调用的时候,后面的代码都不会执行了(request successful 没有被打印),会被抛到全局runqueue里去,然后执行runqueue中等待的goroutine
希望能够帮助和我一样正在学习golang的友军们更好的理解goroutine的调度问题
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
上一篇: sql语句筛选数据分析
下一篇: golang 比较浮点数的大小方式
推荐阅读
-
倒计时cocos定时器schude使用的过程中 帧率浮动较大导致执行时机不准确的问题解决
-
vue.js中v-on:textInput无法执行事件问题的解决过程
-
Ubuntu中启用php的mail()函数并解决发送邮件速度慢问题
-
解决nuxt页面中mounted、created、watch执行两遍的问题
-
Ruby on Rails中jquery_ujs组件拖慢速度的问题解决
-
Golang执行go get私有库提示"410 Gone" 的问题及解决办法
-
flutter中build.gradle仓库的配置(解决外网下载速度过慢失败的问题)
-
解决Golang中goroutine执行速度的问题
-
关于Golang中for-loop与goroutine的问题详解
-
无法执行vbs脚本中遇到的问题及解决方案