关于 goroutine 调度问题

nfwater · 2017-09-01 15:45:05 · 1041 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2017-09-01 15:45:05 的主题,其中的信息可能已经有所发展或是发生改变。

学习 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 先打印?


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1041 次点击  
加入收藏 微博
4 回复  |  直到 2017-09-02 09:54:25
nfwater
nfwater · #1 · 8年之前
package main

import (
    "fmt"
    "runtime"
    "sync"
)

func main() {
    runtime.GOMAXPROCS(1)

    var wg sync.WaitGroup
    wg.Add(2)

    go func() {
        defer wg.Done()
        fmt.Println("A")
    }()

    go func() {
        defer wg.Done()
        fmt.Println("B")
    }()

    wg.Wait()
}

这个例子也是一样,先输出 B,再输出 A,这是为什么?

nfwater
nfwater · #2 · 8年之前
package main

import (
    "fmt"
    "runtime"
    "sync"
)

func main() {
    runtime.GOMAXPROCS(1)

    var wg sync.WaitGroup
    wg.Add(10)

    for index := 1; index <= 10; index++ {
        go func(i int) {
            wg.Done()
            fmt.Println(i)
        }(index)
    }
    wg.Wait()
}

再看这个例子,输出结果: 10 1 2 3 4 5 6 7 8 9

给人的感觉就是始终最先执行最后一个goroutine,再顺序执行前面的goroutine

kangyunqiang
kangyunqiang · #3 · 8年之前

这个顺序跟内部的实现机制有关系,如果把两个函数的位置互换一下,可能又变化了,如果每次循环加上sleep时间,也就更不一样了。

kangyunqiang
kangyunqiang · #4 · 8年之前

这个顺序跟内部的实现机制有关系,如果把两个函数的位置互换一下,可能又变化了,如果每次循环加上sleep时间,也就更不一样了。

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传