当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > goroutine

goroutine

2019年02月22日  | 移动技术网IT编程  | 我要评论
前段时间学习 go, 专门整理的关于 goroutine 的理解. What's goroutine? It's an independently executing function launched by a go statement.It has its own call stack, whi ...

前段时间学习 go, 专门整理的关于 goroutine 的理解.

what's goroutine? it's an independently executing function launched by a go statement.
it has its own call stack, which grows and shrinks as required.
it's very cheap. it's practical to have thousands, even hundreds of thousands of goroutines.
it's not a thread.
there might be only one thread in a program with thousands of goroutines.
instead, goroutines are multiplexed dynamically onto threads as needed to keep all the goroutines running.
but if you think of it as very cheap thread, you won't be far off.

1. 理解"并发" 不是 "并行"

并发: 同时处理一些事
并行: 同时做一些事
concurrency is the composition of independently executing computations.
concurrency is not parallelism, although it enables parallelism.
if you have only one processor, your program can still be concurrent, but it can't be parallel.
on the other hand, a well-written program might run efficiently in parallel on a multiprocessor. that property could be important...
---rob pike

举例: 比如你是一个公司的ceo, 你每天上午会抽出两个小时的时间统一处理回复邮件, 接待下属的各种问题. 那么当你正在回邮件的时候, 有人敲门进来找你签字, 那么你就停下手里的邮件, 把字签了, 然后再继续回来写邮件. 那么在这两个小时里, 你回了好几封邮件, 还签了好几份文件. 而这个过程就可以称为并发. 因为你并没有一边回邮件, 一边签文件, 所以这不是并行. 

2. goroutine 的用法

在需要做并发处理的方法名前面加个 go 关键字即可
首先看下没有引入 goroutine 时的情况

package main
import (
    "fmt"
    "time"
)

func say(s string) {
    for i := 0; i<3; i++ {
        fmt.println(s)
        time.sleep(time.millisecond*100)
    }
}
func main()  {
    say("hey")
    say("there")
}

以下为输出, 这里呢, 就是一个顺序执行, 先执行第一个 say 方法, 循环3次输出, 然后执行第二个say方法, 循环3次输出. 但是这不符合我们的预期, 我希望输出的是交替输出. 也就是输出一个 hey, 输出一个 there ....... 这个需求就有点像我们前面提到的例子, 两个事物需要交替处理

hey
hey
hey
there
there
there 

这样就引出 goroutine 来解决这个并发的问题, 但是具体怎么用, 先看几个代码片段来了解一下:
(1)

func main()  {
     say("hey")
     go say("there")
}

输出, 执行过程: 执行第一个 say() 方法, 连续输出三个 "hey" ---> 执行第二个 say()方法, 而这里由于 goroutine 会立即返回 ---> 程序接着执行后面的代码, 所以第二个方法还没有执行完毕 ---> 程序退出.

hey
hey
hey

(2)

func main()  {
     go say("hey")
     say("there")
}

输出, 执行过程: 执行第一个 say() 方法, 根据 goroutine 立即返回的特性 ---> 执行第二个 say()方法, 输出第一个 there ---> 大家别忘了在 say 方法中, 还有这样一行代码

time.sleep(time.millisecond*100)

它的作用就是模拟缓慢的函数调用, 让程序暂停指定的时间. 多亏了这个延迟, 让第二个循环输出一次之后, 就给了第一个循环的机会 ---> 输出 hey, 之后按照上术逻辑继续循环输出. 

there
hey
there
hey
there
hey

(3)

func main()  {
     go say("hey")
     go say("there")
}

输出, 执行过程: 执行第一个 goroutine, 立即返回 ---> 执行第二个 goroutine, 立即返回 ---> 程序退出, 因此没有任何输出.

process finished with exit code 0

(4)

func main()  {
     go say("hey")
     go say("there")
     // 使程序延迟退出
     time.sleep(time.second)
}

通过 time.sleep 函数使程序延迟一秒退出, 就给了 goroutine 执行的时间, 这里, 我尝试多次刷新, 输出的顺序都不相同, 同时, 还可以尝试把 say 函数里的 time.sleep 去掉, 再多次执行, 也会发现输出的顺序都不同.我觉得这也充分体现了goroutine 的并发执行. 
输出情况一

hey
there
there
hey
there
hey

输出情况二

there
hey
hey
there
hey
there

总结一下 goroutine 的特点: 通过使用 goroutine, 可以在调用函数之后立即执行后面的代码, 但是使用 goroutine 的函数依然会执行, 但不会阻塞程序中其他代码行的执行. 

3. 使用 waitgroup 函数控制程序的退出

在上面的最后一个例子中, 我们使用了 time.sleep 人为控制程序延迟1秒退出, 但是在实际开发中, 我们无法去精准的算出, 所有的 goroutine 完成返回需要多少时间, 所以这样认为控制程序的退出时间, 并不是一个好办法, 因此就引出了 sync 包的 waitgroup 函数. 使用方法详见注释如下:

package main
import (
    "fmt"
    // 引入sync 包
    "sync"
    "time"
)
// 声明 waitgroup 类型的变量 wg 
var wg sync.waitgroup
func say(s string) {
   for i := 0; i<3; i++ {
       fmt.println(s)
       time.sleep(time.millisecond*100)
   }
   // 告知所有的 goroutine 运行结束, 程序可以返回了
   wg.done()
}

func main()  {
    //添加一个 goroutine 到 waitgroup
    wg.add(1)
     go say("hey")
    //添加一个 goroutine 到 waitgroup
    wg.add(1)
     go say("there")
    // 告诉程序等待以上添加到 waitgroup 的 goroutine 运行结束再向下执行
    wg.wait()
    
    -----以上可以看作是以wg.wait()为分割的第一个 waitgroup, 以下为第二个----
    
    //添加一个 goroutine 到另一个 waitgroup
    wg.add(1)
    go say("hi")
    // 告诉程序等待这个 waitgroup 里的 goroutine 运行结束再向下执行
    wg.wait()
}

输出结果如下

there
hey
there
hey
there
hey
hi
hi
hi

4. defer

defer statement will defer running the function until the end of the surrounding function, either that the function is done, or if the function does happened error panic out, the defer statement will run.
defer 遵循先进后出原则, 所以是倒叙执行
在上面的例子中, 我们看到 wg.done() 函数是写在 for 循环后面的, 那么如果前面执行中如果出了错误, 程序就永远无法执行到 wg.done() 函数, 那么整个程序就会无限制地等下去, 对于这个问题, 我们可以把 wg.done() 函数写在 defer 语句后面, 而且放在函数的最前面,这样不管后面的执行是正常返回, 还是报错, 都会最终执行到 wg.done() 函数. 这样才能正常向下执行.

func say(s string) {
    // defer 语句会确保函数 wg.done() 执行,且是在函数内其他程序之后
    defer wg.done() // 告知所有的 goroutine 运行结束, 程序可以返回
    for i := 0; i<3; i++ {
        fmt.println(s)
        time.sleep(time.millisecond*100)
    }
}

5. panic 和 recover

func cleanup() {
    defer wg.done()
    if r := recover(); r != nil {
        fmt.println("recover from cleanup:", r)
    }
}
func say(s string) {
    defer cleanup()
    for i := 0; i<3; i++ {
        fmt.println(s)
        time.sleep(time.millisecond*100)
        if i == 1 {
           panic("oh my god")
        }
    }
}

func main()  {
    //添加一个 goroutine 到 waitgroup
    wg.add(1)
     go say("hey")
    //添加一个 goroutine 到 waitgroup
    wg.add(1)
     go say("there")
    // 告诉程序等待 goroutine 运行
    wg.wait()

    fmt.println("this is something")
    
    //添加一个 goroutine 到 waitgroup
    wg.add(1)
    go say("hi")
    // 告诉程序等待 goroutine 运行
    wg.wait()
}

运行结果, 可以看到由于在 i=1 的时候, 运行 panic()函数, 所以只能循环两次, for 循环遇到 panic 就会停止执行, 然后运行被 defer 的 cleanup()函数, 在这个函数里, 调用了 recover()函数, 调用 recover 函数, 就会得到 panic()函数的返回值, 我们将此值赋值给了变量 r, 当 r 不等于 nil 的时候, 打印它. 最后再执行被 defer 的 wg.done() 函数, 告知程序向下执行.

there
hey
there
hey
recover from cleanup: oh my god
recover from cleanup: oh my god
this is something
hi
hi
recover from cleanup: oh my god

6. channel

package main

import (
    "fmt"
    "sync"
)

var wg sync.waitgroup

// 用于向通道里传值的函数
func foo(c chan int, val int) {
    wg.done()
    //把接收到的值乘以5后传给指定的通道
    c <- val * 5
}
func main() {
    // 创建一个通道, buffer 为10, 也就是可以缓冲 10个值
    fooval := make(chan int, 10)
    //循环10次调用 foo()函数, 也就是向 fooval 通道里传了10个值
    for i := 1; i < 10; i++ {
        wg.add(1)
        go foo(fooval, i)
    }
    //等待向通道传值的 goroutine 全部完成
    wg.wait()
    // 通道用完, 需要及时关闭
    close(fooval)

    // 把通道里的值循环打印出来
    for item := range fooval {
        fmt.println(item)
    }
}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网