"fmt"
"runtime"
"time"
)
func main() {
runtime.GOMAXPROCS(1)
go func() {
fmt.Println("a")
}()
go func() {
fmt.Println("b")
}()
go func() {
fmt.Println("c")
}()
fmt.Println("d")
select {
case _ = <-time.After(time.Second):
}
}
result:dabc
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(1)
go func() {
fmt.Printf("a")
}()
go func() {
fmt.Printf("b")
}()
go func() {
fmt.Printf("c")
}()
fmt.Printf("d")
select {}
}
result:dcab
Because I set "runtime.GOMAXPROCS(1)", there's only one "Processor" in my program.
One "Processor" has only one "runq", and in/out operation is "FIFO"
My question is "why always... not rand...."
Finally figured it out.
Processor.runq is FIFO, there's another variable(runnext) here, runnext saves the lastest goroutine which where will be processed next.
The latest goroutine kicks p.runnext
Timer creates a goroutine ,and this goroutine kicks p.runnext.
Processor的runnext 被定时器创建的goroutine kick调, 导致下一个执行的是定时器的goroutine,然后其他的FIFO