当前位置:网站首页>Go从入门到实战——仅需任意任务完成(笔记)
Go从入门到实战——仅需任意任务完成(笔记)
2022-06-27 19:23:00 【积淀 ytu】
使用无缓冲的channel
func runTask(id int) string {
time.Sleep(10 * time.Millisecond)
return fmt.Sprintf("The result is from %d", id)
}
func FirstResponse() string {
numOfRunner := 10
ch := make(chan string)
for i := 0; i < numOfRunner; i++ {
go func(i int) {
ret := runTask(i)
ch <- ret
}(i)
}
return <-ch
}
以上方法协程数量不会因为输出结果之后,所有协程都释放,因为还有很多channel处于阻塞状态
使用有缓存的channel
func runTask(id int) string {
time.Sleep(10 * time.Millisecond)
return fmt.Sprintf("The result is from %d", id)
}
func FirstResponse() string {
numOfRunner := 10
ch := make(chan string,numOfRunner)
for i := 0; i < numOfRunner; i++ {
go func(i int) {
ret := runTask(i)
ch <- ret
}(i)
}
return <-ch
}
边栏推荐
- 如何将队列里面的内容没秒钟执行一次优先级
- gomock mockgen : unknown embedded interface
- 2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
- Tutorial | fNIRS data processing toolkit homer2 download and installation
- 覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎
- This is the same as data collection. Can you define a parameter as last month or the previous day, and then use this parameter in SQL?
- 100 important knowledge points that SQL must master: retrieving data
- 抖音的兴趣电商已经碰到流量天花板?
- 爱数课实验 | 第七期-基于随机森林的金融危机分析
- 小王的面试训练任务
猜你喜欢

覆盖接入2w+交通监测设备,EMQ 为深圳市打造交通全要素数字化新引擎

JPA踩坑系列之save方法

数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践

squid代理服务器

银河麒麟系统局域网文件共享教程

AI 绘画极简教程

Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)

创建对象时JVM内存结构

Modify large online games through CE modifier

Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
随机推荐
100 important knowledge points for SQL: in operator
空指针异常
1027 Colors in Mars
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
数组作业题
Flexible IP network test tool -- x-launch
Share how I take notes
Love math experiment | phase 9 - intelligent health diagnosis using machine learning method
SQL必需掌握的100个重要知识点:用通配符进行过滤
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
Squid proxy server
Acwing周赛57-数字操作-(思维+分解质因数)
爱数课实验 | 第七期-基于随机森林的金融危机分析
事件相关电位ERP的皮层溯源分析
VMware vSphere esxi 7.0 installation tutorial
白嫖红队goby&POC,叫你如何白嫖?
银河麒麟系统局域网文件共享教程
100 important knowledge points that SQL must master: filtering data
Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml