Execution sequence control method of Goroutines and Channels in Golang
In Golang programming, Goroutine and Channel are two very important concepts. Goroutine is a lightweight thread that can run multiple functions simultaneously during the execution of a program. Channel is a mechanism used for communication between Goroutines.
In some cases, we need to control the execution order of Goroutine and Channel to ensure that the program runs as we expect. This article will introduce some common methods to implement sequence control of Goroutine and Channel.
WaitGroup is a counter used to wait for the end of a group of Goroutines. Its working principle is that every time a Goroutine is started, the counter is incremented by one; after each Goroutine is executed, the counter is decremented by one. When the counter reaches zero, the main thread continues execution.
The following is a sample code using WaitGroup for sequence control:
package main import ( "fmt" "sync" ) func main() { var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() fmt.Println("Goroutine 1") }() go func() { defer wg.Done() fmt.Println("Goroutine 2") }() wg.Wait() fmt.Println("Main Goroutine") }
In this example, we create two Goroutines, each Goroutine prints a message. In each Goroutine, we decrement the counter by one with thedefer wg.Done()
statement. Finally,wg.Wait()
will block the main thread until all Goroutines are executed.
Run the above code, the output is:
Goroutine 1 Goroutine 2 Main Goroutine
You can see that the execution order of Goroutine 1 and Goroutine 2 is uncertain, but they must be before the main thread prints "Main Goroutine" Finish.
An unbuffered Channel can be used to block the execution of a Goroutine until another Goroutine receives data from the Channel. By building a Goroutine dependency chain, we can execute multiple Goroutines sequentially.
The following is a sample code using an unbuffered Channel for sequential control:
package main import ( "fmt" ) func main() { ch1 := make(chan struct{}) ch2 := make(chan struct{}) go func() { fmt.Println("Goroutine 1") ch1 <- struct{}{} }() go func() { <-ch1 fmt.Println("Goroutine 2") ch2 <- struct{}{} }() <-ch2 fmt.Println("Main Goroutine") }
In this example, after Goroutine 1 prints a message, it sends an empty structure to the ch1 Channel. Next, Goroutine 2 prints another message after receiving the data from ch1 Channel, and sends an empty structure to ch2 Channel. Finally, the main thread prints "Main Goroutine" after receiving data from ch2 Channel.
Run the above code, the output result is:
Goroutine 1 Goroutine 2 Main Goroutine
You can see that the execution order of Goroutine 1 and Goroutine 2 is determined, and they must be completed before the main thread prints "Main Goroutine".
A buffered Channel allows us to specify a capacity when creating it, which can store a certain amount of data. By setting the buffer size appropriately, we can control the number of concurrent executions of Goroutine and achieve sequence control.
The following is a sample code for sequential control using a buffered Channel:
package main import ( "fmt" ) func main() { ch := make(chan struct{}, 2) go func() { fmt.Println("Goroutine 1") ch <- struct{}{} }() go func() { fmt.Println("Goroutine 2") ch <- struct{}{} }() <-ch <-ch fmt.Println("Main Goroutine") }
In this example, we create a ch Channel with a buffer of capacity 2. In each Goroutine, we send an empty structure to the ch Channel. Finally, the main thread prints "Main Goroutine" after receiving data twice from ch Channel.
Run the above code, the output result is:
Goroutine 1 Goroutine 2 Main Goroutine
You can see that the execution order of Goroutine 1 and Goroutine 2 is determined, and they must be completed before the main thread prints "Main Goroutine".
Through the above method, we can flexibly control the execution order of Goroutine and Channel. According to actual needs, choosing the appropriate method can improve the concurrency and efficiency of the program.
Summary:
This article introduces three common methods to control the execution order of Goroutine and Channel in Golang: using WaitGroup, using unbuffered Channel and using buffered Channel. By rationally using these methods, flexible concurrency control can be achieved and the performance and efficiency of the program can be improved.
The above is the detailed content of Execution order control method of Goroutines and Channels in Golang. For more information, please follow other related articles on the PHP Chinese website!