1
去哪儿面试题 (Golang实现)多线程顺序输出1-75
source link: https://studygolang.com/articles/20961?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
去哪儿面试题 (Golang实现)多线程顺序输出1-75
FredricZhu · 2019-06-06 19:32:39 · 2248 次点击 · 预计阅读时间不到 1 分钟 · 大约8小时之前 开始浏览这是一个创建于 2019-06-06 19:32:39 的文章,其中的信息可能已经有所发展或是发生改变。
package main
import (
"fmt"
"sync"
"time"
)
var waiter sync.WaitGroup
func print5(i int) {
j := i
for j < i+5 {
fmt.Println(j)
j++
}
waiter.Done()
}
func main() {
fmt.Println("Start print 75")
waiter = sync.WaitGroup{}
waiter.Add(15)
for i := 1; i < 75; i += 5 {
go print5(i)
time.Sleep(1 * time.Nanosecond)
}
waiter.Wait()
fmt.Println("End print 75")
}
程序输出如下,
image.png
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK