• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Golang > Golang中的sync.WaitGroup用法实例

Golang中的sync.WaitGroup用法实例

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了sync.waitgroup,go sync.waitgroup,golang sync,golang sync.pool,golang 实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

WaitGroup的用途:它能够一直等到所有的goroutine执行完成,并且阻塞主线程的执行,直到所有的goroutine执行完成。

官方对它的说明如下:

A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished.

sync.WaitGroup只有3个方法,Add(),Done(),Wait()。

其中Done()是Add(-1)的别名。简单的来说,使用Add()添加计数,Done()减掉一个计数,计数不为0, 阻塞Wait()的运行。

 
例子代码如下:

同时开三个协程去请求网页, 等三个请求都完成后才继续 Wait 之后的工作。

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(1) 
  // 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()

 
</div>

或者下面的测试代码

用于测试 给chan发送 1千万次,并接受1千万次的性能。

package main

import ( 
  "fmt" 
  "sync" 
  "time" 
)

const ( 
  num = 10000000 
)

func main() { 
  TestFunc("testchan", TestChan) 
}

func TestFunc(name string, f func()) { 
  st := time.Now().UnixNano() 
  f() 
  fmt.Printf("task %s cost %d \r\n", name, (time.Now().UnixNano()-st)/int64(time.Millisecond)) 
}

func TestChan() { 
  var wg sync.WaitGroup 
  c := make(chan string) 
  wg.Add(1)

  go func() { 
    for _ = range c { 
    } 
    wg.Done() 
  }()

  for i := 0; i < num; i++ { 
    c <- "123" 
  }

  close(c) 
  wg.Wait()

}
</div>

</div>

您可能想查找下面的文章:

  • Golang中的sync.WaitGroup用法实例

相关文章

  • Golang中channel使用的一些小技巧
  • 剖析Go编写的Socket服务器模块解耦及基础模块的设计
  • Go语言中嵌入C语言的方法
  • Go语言计算两个经度和纬度之间距离的方法
  • Golang极简入门教程(三):并发支持
  • Go语言执行系统命令行命令的方法
  • go语言日志记录库简单使用方法实例分析
  • Go语言基础知识总结(语法、变量、数值类型、表达式、控制结构等)
  • 简单讲解Go程序中使用MySQL的方法
  • Go语言写入字符串到文件的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • go语言map字典删除操作的方法
    • Go语言用map实现堆栈功能的方法
    • Go项目的目录结构详解
    • Go语言算法之寻找数组第二大元素的方法
    • go语言异常panic和恢复recover用法实例
    • 深入剖析Go语言编程中switch语句的使用
    • GO语言实现的端口扫描器分享
    • Go语言常见哈希函数的使用
    • Golang继承模拟实例详解
    • Go语言对字符串进行SHA1哈希运算的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有