In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the ways to manage Concurrency in Go language". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "what are the ways to manage Concurrency in Go language" together!
WaitGroup
Let's first understand what situation you need to use WaitGroup. Suppose you have two machines that need to upload the latest code at the same time. After the two machines upload separately, you can execute the final restart step. It's like splitting a job into several parts at the same time, which can reduce time, but in the end, you need to wait until all of them are done before you can execute the next step. At this time, you need to use WaitGroup to do it.
package mainimport ( "fmt" "sync")func main() { var wg sync.WaitGroup i := 0 wg.Add(3) //task count wait to do go func() { defer wg.Done() // finish task1 fmt.Println("goroutine 1 done") i++ }() go func() { defer wg.Done() // finish task2 fmt.Println("goroutine 2 done") i++ }() go func() { defer wg.Done() // finish task3 fmt.Println("goroutine 3 done") i++ }() wg.Wait() // wait for tasks to be done fmt.Println("all goroutine done") fmt.Println(i)}Channel
Another practical case is when we need to actively notify a Goroutine to stop. In other words, when the App starts, it will run some monitoring programs in the background, and when the whole App needs to stop before, you need to send a Notification to the monitoring program in the background to stop it first, then you need to use Channel to notify. Consider the following example:
package mainimport ( "fmt" "time")func main() { exit := make(chan bool) go func() { for { select { case
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.