go等待⼀组协程结束的操作⽅式
go提供了sync包和channel来解决协程同步和通讯。
⽅式1:
sync.WaitGroup是等待⼀组协程结束,sync.WaitGroup只有3个⽅法,Add()添加⼀个计数,Done()减去⼀个计数,Wait()阻塞直到所有任务完成。
package main
import (
"fmt"
"sync"
"time"
)
var wg sync.WaitGroup //定义⼀个同步等待的组
func task(i int){
fmt.Println("",i)
//耗时操作任务,⽹络请求,读取⽂件
time.Sleep(time.Second)
wg.Done() //减去⼀个计数
}
func main(){
for i:= 0;i<10;i++{
wg.Add(1) //添加⼀个计数
go task(i)
}
wg.Wait() //阻塞直到所有任务完成
fmt.Println("over")
}
运⾏结果:
task (9)
task (4)
task (6)
task 0
task (7)
task (5)
task (1)
task (2)
task (8)
task (3)
over
⽅式2:
利⽤缓冲信道channel协程之间通讯,其阻塞等待功能实现等待⼀组协程结束,不能保证其goroutine按照顺序执⾏
package main
import (
"fmt"
)
var ch = make(chan int,10)
func task(i int){
fmt.Println("",i)
ch <- i
}
func main(){
for i:= 0;i<10;i++{
go task(i)
}
for i:= 0;i<10;i++{
<- ch
}
fmt.Println("over")
}
运⾏结果:
task (9)
task 0
task (1)
task (2)
task (6)
task (7)
task (3)
task (4)
task (8)
task (5)
over
⽅式3:
利⽤⽆缓冲的信道channel协程之间通讯,其阻塞等待功能实现等待⼀组协程结束,保证了其goroutine按照顺序执⾏package main
import (
"fmt"
"time"
)
var ch = make(chan int)
func task(i int){
fmt.Println("",i)
time.Sleep(time.Second)
<- ch
}
func main(){
for i:= 0;i<10;i++{
go task(i)
ch <- i
}
fmt.Println("over")
}
运⾏结果:
task 0
task (1)
task (2)
task (3)
task (4)
task (5)
task (6)
task (7)
task (8)
task (9)
over最美消防员
补充:Go中使⽤Channel等待所有协程结束
放飞的心让main⽅法等待所有协程执⾏完毕再退出。可能⼀般思路是设置⼀个共有变量,然后通过修改这个变量的状态。这是通过共享变量来通信的⽅式,⽽go要做的是,通过通信来共享内存。
1. 按顺序执⾏
每次通信进⾏成对通信,当main向协程发送⼀个写channel时,同时也等待协程返回⼀个读channel。
这两个channel⼀定是成对的,所以构造⼀个结构体
type worker struct {
in chan int
done chan bool
}
func chanDemo1(){
var workers [10]worker
for i := 0; i < 10; i++ {
workers[i] = createWorker1(i)
}
for i := 0; i < 10; i++ {
workers[i].in <- 'a' + i
<- workers[i].done
}
for i := 0; i < 10; i++ {
workers[i].in <- 'A' + i
<- workers[i].done
}
}
func createWorker1(id int) worker {
work := worker{
in: make(chan int),
done: make(chan bool),
}
go func() {
for {
fmt.Printf("Work %d receiverd %c\n", id, <- work.in)
work.done <- true
}
}()
return work
}
func main(){
chanDemo1()
fmt.Println("over")
}
这个执⾏结果完全是按照0-9,先⼩写再⼤写的顺序
如果这样顺序执⾏,还要协程⼲啥
2. 批量处理
平果县type worker struct {
in chan int
done chan bool
}
func chanDemo1(){
var workers [10]worker
for i := 0; i < 10; i++ {
workers[i] = createWorker1(i)
愚人节几号}
for i := 0; i < 10; i++ {
workers[i].in <- 'a' + i
}
for _, worker := range workers {
<- worker.done
}
for i := 0; i < 10; i++ {
workers[i].in <- 'A' + i
}
for _, worker := range workers {
<- worker.done
}
}
func createWorker1(id int) worker {
work := worker{
in: make(chan int),
done: make(chan bool),
}
go func() {
for {
fmt.Printf("Work %d receiverd %c\n", id, <- work.in)
work.done <- true
}
}()
return work
}
这样的话,先打印⼩写,再打印⼤写,但是⼤⼩写时顺序不固定3. 完全随机
火龙果怎么种
func chanDemo1(){
var workers [10]worker
for i := 0; i < 10; i++ {
workers[i] = createWorker1(i)
}
for i := 0; i < 10; i++ {
workers[i].in <- 'a' + i
}
for i := 0; i < 10; i++ {
workers[i].in <- 'A' + i
}
for _, worker := range workers {
<- worker.done
<- worker.done
概成语}
}
func createWorker1(id int) worker {
work := worker{
in: make(chan int),
怎么做饺子done: make(chan bool),
}
go func() {
for {
fmt.Printf("Work %d receiverd %c\n", id, <- work.in)
// 再开⼀个协程
go func() { work.done <- true}()
}
}()
return work
}
这种⽅式就是完全随机了
使⽤channel进⾏树的遍历
func (node *Node) TraverFunc(f func(*Node)){
if node == nil{
return
}
node.Left.TraverFunc(f)
f(node)
node.Right.TraverFunc(f)
}
func (node *Node) TraverWithChannel() chan *Node{
out := make(chan *Node)
go func() {
node.TraverFunc(func(node *Node) {
out <- node
})
clo(out)
}()
return out
}
func main(){
var root Node
root = Node{Value:3}
root.Left = &Node{}
root.Right = &Node{5,nil,nil}
root.Right.Left = new(Node)
root.Left.Right =&Node{6,nil,nil}
root.Traver()
c:=root.TraverWithChannel()
maxNode := 0
for node := range c{
大上if node.Value > maxNode{
maxNode = node.Value
}
}
fmt.Println("max node value:", maxNode)
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。如有错误或未考虑完全的地⽅,望不吝赐教。