In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to realize concurrency control in Golang? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
A goroutine can be opened with the go keyword in Golang, so concurrent code can be easily written in Go. But how to effectively control these concurrently executed groutines?
When it comes to concurrency control, locks are probably the first thing that comes to mind. Lock-related mechanisms are also provided in Golang, including mutex sync.Mutex and read-write lock sync.RWMutex. In addition to locks, there are atomic operations such as sync/atomic. However, these mechanisms focus on the concurrent data security of goroutines. What this article wants to discuss is the concurrency behavior control of goroutine.
There are three common ways to control the concurrency behavior of goroutine, which are WaitGroup, channel and Context.
WaitGroup
WaitGroup is located under the sync package, and it is used as follows.
Func main () {var wg sync.WaitGroup
Wg.Add (2) / / add workload to be completed 2
Go func () {wg.Done () / complete workload 1 fmt.Println ("goroutine 1 finish work!") } ()
Go func () {wg.Done () / complete workload 1 fmt.Println ("goroutine 2 finish work!") } ()
Wg.Wait () / waiting for workload 2 to complete fmt.Println ("all goroutine has finished work!") }
Output: / / goroutine 2 to complete the work! / / goroutine 1 finish the work! / / all goroutine have finished their work!
WaitGroup, a concurrency control method, is especially suitable for a task that requires multi-goroutine cooperation, and each goroutine can only do part of the task. Only when all the goroutine is completed, the task can be considered complete. Therefore, WaitGroup, like the name, is a way of waiting.
However, in the actual business, there is such a scenario: when a requirement is met, it is necessary to actively notify a certain goroutine to end. For example, if we start a background monitoring goroutine, when monitoring is no longer needed, we should notify the monitoring goroutine to end, otherwise it will keep idling and cause leakage.
Channel
There is nothing WaitGroup can do about the above scenario. The easiest way you can think of: define a global variable, notify it by modifying it elsewhere, and the background goroutine will constantly check the variable and turn it off if it is found to have changed, but this method is a bit clumsy. In this case, channel+select can come in handy.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.