关于 goroutine 调度问题
发布时间:2022-03-27 编辑:jiaochengji.com
教程集为您提供关于 goroutine 调度问题等资源,欢迎您收藏本站,我们将为您提供最新的关于 goroutine 调度问题资源
<textarea id="append-test" style="display:none;">学习 goroutine 的时候看过这个例子,不太明白为什么会这样?请看代码:
```
// This sample program demonstrates how to create goroutines and
// how the scheduler behaves.
package main
import (
"fmt"
"runtime"
"sync"
)
// main is the entry point for all Go programs.
func main() {
// Allocate 1 logical processor for the scheduler to use.
runtime.GOMAXPROCS(1)
// wg is used to wait for the program to finish.
// Add a count of two, one for each goroutine.
var wg sync.WaitGroup
wg.Add(2)
fmt.Println("Start Goroutines")
// Declare an anonymous function and create a goroutine.
go func() {
// Schedule the call to Done to tell main we are done.
defer wg.Done()
// Display the alphabet three times
for count := 0; count < 3; count {
for char := 'a'; char < 'a' 26; char {
fmt.Printf("%c ", char)
}
}
}()
// Declare an anonymous function and create a goroutine.
go func() {
// Schedule the call to Done to tell main we are done.
defer wg.Done()
// Display the alphabet three times
for count := 0; count < 3; count {
for char := 'A'; char < 'A' 26; char {
fmt.Printf("%c ", char)
}
}
}()
// Wait for the goroutines to finish.
fmt.Println("Waiting To Finish")
wg.Wait()
fmt.Println("\nTerminating Program")
}
```
这段代码为什么始终先输出第二个 goroutine 的内容,即大写字母,再输出第一个 goroutine 的内容小写字母?
goroutine 不是无序的吗,为什么这个例子总是顺序一一,并且第二个 goroutine 先打印?</textarea>
到此这篇关于“关于 goroutine 调度问题”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!
您可能感兴趣的文章:
Goroutine的调度分析(一)
简单理解 Goroutine 是如何工作的
goroutine 调度器
关于 goroutine 调度问题
golang 深入浅出之 goroutine 理解
golang中goroutine的调度
GO 语言之 Goroutine 原理解析
正在执行的goruntine发生阻塞,golang调度策略
Goroutine是如何工作的
21面试必问!Goroutine的调度原理
上一篇:go语言 json 问题
下一篇:golang 以及 go SDK 交叉编译
[关闭]