go协程控制方式有 waitGroup, select+chan(类似epoll), context三种。
waitGroup类似于消费者模型,使用ADD() 方法指定初始产品数量,使用Done()方法消费一个物品,使用Wait()表示直到物品数量消费到0为止 ,否则一直阻塞。
select就是对管道进行选择,哪个管道中写入了数据就选择哪个,多个同时写入的话随机选择。
context即上下文,使用
ctx, cancel := context.WithCancel(context.Background())
ctx传入到要监控的 协程中, cancel供控制协程使用。ctx.Done()返回一个只读的channel。
当调用cancel()时, ctx.Done() 这一方法返回的管道就会别写入。
代码如下:
package main
import (
"context"
"fmt"
"time"
)
func watch(ctx context.Context, name string){
for{
select {
// 当cancel调用时 Done 返回一个struct{}
case <- ctx.Done():
fmt.Println("exit watch")
return
default:
// print format
fmt.Printf("%s is watching\n", name)
time.Sleep(2*time.Second)
}
}
}
func main(){
ctx, cancel := context.WithCancel(context.Background())
// cancel 是个函数 ctx 是传到调用函数中的 cancel是在哪里取消哪里调用
// 用来控制多个协程的一种方式
go watch(ctx, "watch 1")
go watch(ctx, "watch 2")
go watch(ctx, "watch 3")
time.Sleep(10*time.Second)
fmt.Println("stop watching")
cancel()
}