当前位置:网站首页>goroutine
goroutine
2022-04-23 19:21:00 【Handsome that handsome】
The original version :
func add(a int) int {
// Assume that processing business logic requires 100ms
time.Sleep(time.Millisecond * 100)
return a
}
func main() {
sum := 0
start := time.Now().UnixNano() / 1e6
for i := 0; i < 100; i++ {
sum += add(i)
}
end := time.Now().UnixNano() / 1e6
fmt.Printf(" Time consuming :%d\n", end-start)
fmt.Println(sum)
}
result :
Time consuming :11012
4950
Modified code
func add1(a int,allocatChan chan int) {
time.Sleep(time.Millisecond * 100)
allocatChan<-a
}
func main() {
var sum int = 0
var allocatChan chan int = make(chan int,100)
var flag int = 0
start := time.Now().UnixNano() / 1e6
for i := 0; i < 100; i++ {
go add1(i,allocatChan)
}
for{
if flag == 100{
break
}
flag+=1
a:=<-allocatChan
sum+=a
}
end := time.Now().UnixNano() / 1e6
fmt.Printf(" Time consuming :%d\n", end-start)
fmt.Println(sum)
}
result :
Time consuming :90
4950
版权声明
本文为[Handsome that handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210557451077.html
边栏推荐
- JS to get the local IP address
- JVM的类加载过程
- Raspberry pie 18b20 temperature
- JVM的类加载过程
- 深度学习环境搭建步骤—gpu
- SSDB基础1
- OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
- 数据分析学习目录
- Regular expressions for judging positive integers
- Easy mock local deployment (you need to experience three times in a crowded time. Li Zao will do the same as me. Love is like a festival mock)
猜你喜欢
MySQL syntax collation (2)
I just want to leave a note for myself
ArcMap publishing slicing service
命令-sudo
MySQL syntax collation (3)
8266 obtain 18b20 temperature
An idea of rendering pipeline based on FBO
Intuitive understanding of the essence of two-dimensional rotation
Client interns of a large factory share their experience face to face
Oracle configuration st_ geometry
随机推荐
c1000k TCP 连接上限测试1
Wechat video extraction and receiving file path
redis优化系列(三)解决主从配置后的常见问题
8266 obtain 18b20 temperature
FTP、ssh远程访问及控制
什么是消息队列
SSDB基础
The flyer realizes page Jump through routing routes
2021-2022-2 ACM集训队每周程序设计竞赛(8)题解
Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy
Machine learning catalog
JS to get the local IP address
SSDB foundation 2
2021-2022-2 ACM training team weekly Programming Competition (8) problem solution
Openlayers 5.0 thermal diagram
JS controls the file type and size when uploading files
Strange passion
坐标转换WGS-84 转 GCJ-02 和 GCJ-02转WGS-84
ArcMap connecting ArcGIS Server
[transfer] summary of new features of js-es6 (one picture)