Golang 学习笔记

Golang:协程

2018-01-18  本文已影响243人  与蟒唯舞

协程(Goroutine)是与其他函数或方法同时运行的函数或方法。可以认为它是轻量级的线程。与线程相比,创建 Goroutine 的成本很小。因此,Go 应用程序可以有上千个 Goroutines 同时运行。

在函数或方法调用之前加上关键字 go,这样便开启了一个并发的 Goroutine。

package main

import (  
    "fmt"
)

func hello() {  
    fmt.Println("Hello world goroutine")
}
func main() {  
    go hello()
    fmt.Println("main function")
}

go hello() 开启了一个新的协程。现在 hello() 函数将和 main() 函数一起运行。main() 函数在单独的协程中运行,这个协程称为主协程。

运行上面的程序将仅输出一行文本: main function

原因:

package main

import (  
    "fmt"
    "time"
)

func hello() {  
    fmt.Println("Hello world goroutine")
}
func main() {  
    go hello()
    time.Sleep(1 * time.Second)
    fmt.Println("main function")
}

开启多个协程:

package main

import (  
    "fmt"
    "time"
)

func numbers() {  
    for i := 1; i <= 5; i++ {
        time.Sleep(250 * time.Millisecond)
        fmt.Printf("%d ", i)
    }
}
func alphabets() {  
    for i := 'a'; i <= 'e'; i++ {
        time.Sleep(400 * time.Millisecond)
        fmt.Printf("%c ", i)
    }
}
func main() {  
    go numbers()
    go alphabets()
    time.Sleep(3000 * time.Millisecond)
    fmt.Println("main terminated")
}

下面的图片描述了这个程序是如何工作的:

上一篇 下一篇

猜你喜欢

热点阅读