Go并发控制之sync.WaitGroup
WaitGroup 会将main goroutine阻塞直到所有的goroutine运行结束,从而达到并发控制的目的。使用方法非常简单,真心佩服创造Golang的大师们!
type WaitGroup //相当于一个箱子,将main goroutine 保护到里面
func (*WaitGroup) Add //调用一次为箱子加一把锁(当然,你愿意也可以多把)
func (*WaitGroup) Done // 调用一次开一把锁(only one!)
func (*WaitGroup) Wait //箱子的盖子,没锁了自动打开
官网说明:一个WaitGroup锁等待一个goroutines合集结束。main goroutine里面调用Add方法设置需要等待的goroutines 数量,然后运行每一个goroutine,并且当其结束时调用Done方法。同时,main goroutine 被锁住直到所有的goroutines完成。
使用方法(官网Example):
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Increment the WaitGroup counter.
wg.Add()
// Launch a goroutine to fetch the URL.
go func(url string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
// Fetch the URL.
http.Get(url)
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
任何放在wg.Wait() 后面的语句阻塞,直到所有的goroutine返回:
package main import (
"fmt"
"net/http"
"sync"
) func main() {
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Increment the WaitGroup counter.
wg.Add()
// Launch a goroutine to fetch the URL.
go func(url string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
// Fetch the URL.
http.Get(url)
fmt.Println("我先干活, 主程序等着我")
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
fmt.Println("应该最后才出来") }
Go并发控制之sync.WaitGroup的更多相关文章
- sync—WaitGroup
用途:阻塞主线程的执行,直到所有的goroutine执行完成 WaitGroup总共有三个方法:Add(delta int),Done(),Wait().简单的说一下这三个方法的作用. Add:添加或 ...
- sync.WaitGroup和sync.Once
sync.WaitGroup,顾名思义,等待一组goroutinue运行完毕.sync.WaitGroup声明后即可使用,它有如下方法: func (wg *WaitGroup) Add(delta ...
- golang 的 sync.WaitGroup
WaitGroup的用途:它能够一直等到所有的goroutine执行完成,并且阻塞主线程的执行,直到所有的goroutine执行完成. 官方对它的说明如下: A WaitGroup waits for ...
- 《Go语言实战》笔记之协程同步 sync.WaitGroup
原文地址(欢迎互换友链): http://www.niu12.com/article/8 sync 包提供同步 goroutine 的功能 <p>文档介绍</p><cod ...
- golang-----golang sync.WaitGroup解决goroutine同步
go提供了sync包和channel来解决协程同步和通讯.新手对channel通道操作起来更容易产生死锁,如果时缓冲的channel还要考虑channel放入和取出数据的速率问题. 从字面就可以理解, ...
- golang sync.WaitGroup
//阻塞,直到WaitGroup中的所以过程完成. import ( "fmt" "sync" ) func wgProcess(wg *sync.WaitGr ...
- Golang sync.WaitGroup的用法
0x01 介绍 经常会看到以下了代码: 12345678910111213 package main import ( "fmt" "time") func m ...
- Golang的sync.WaitGroup 实现逻辑和源码解析
在Golang中,WaitGroup主要用来做go Routine的等待,当启动多个go程序,通过waitgroup可以等待所有go程序结束后再执行后面的代码逻辑,比如: func Main() { ...
- golang的sync.WaitGroup使用示例
下面一段代码 len(m) 不一定会打印为 10,为什么?.如果想要 len(m) 打印为 10,应该怎么修改代码? func main() { const N = 10 m := make(map[ ...
随机推荐
- 【Junit 报错】No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
Junit报错 log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvi ...
- tangram2.6(XE2)\Demo\notify\notifyGroup.groupproj
1.以下此异常,为exe没有加载到Tangram_Core.bpl 放到exe当前文件夹下即可 2.此例子的接口实现在exe中,exe中下发通知到dll,dll 中 as 获取接口传窗体到exe中: ...
- iOS 设置1像素的UIView线
如果是代码实现,直接 在CGRectMake里把对应的参数设置为: 1.0/[UIScreenmainScreen].scale 即可. 如果是用xib实现,就需要将对应的限制拖一个I ...
- 【oracle】oracle表结构导出到Word
因为需要写数据库文档,所以需要把数据库里边的表结构在word中用表格列出来,之前一直用powerdesigner,感觉有些麻烦,后来在网上找到了一段sql语句,经测试完全符合我的需求,不敢独享,语句如 ...
- html上下结构(上部固定高度,下部平铺)
html页面上下结构: 上部固定,下部平铺 <div id="page_header"></div> <div id="page_conte ...
- an excellent capability of C# language and compiler
Sometimes you want to write code that works for different primitive types, and as C# doesn't support ...
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- ffplay代码播放pcm数据
摘抄雷兄 http://blog.csdn.net/leixiaohua1020/article/details/46890259 /** * 最简单的SDL2播放音频的例子(SDL2播放PCM) * ...
- java.lang.NoClassDefFoundError: javax/el/ELResolver 问题解决
HTTP Status 500 - java.lang.NoClassDefFoundError: javax/el/ELResolver type Exception report message ...
- [canvas]利用canvas绘制自适应的折线图
前段时间学习了用canvas绘制折现图,且当画布变换大小,折现图会随之变化,现附上代码 <!DOCTYPE html> <html lang="en"> & ...