Concurrent execution of tasks in Golang and Go WaitGroup
In Golang, the concurrent execution of tasks is very important. By executing tasks concurrently, you can improve the efficiency and responsiveness of your program. Golang provides some features that make concurrent execution of tasks simple and efficient. One of the important features is Go WaitGroup.
Go WaitGroup is a synchronization primitive in Golang, used to wait for a group of coroutines (goroutines) to complete execution. It ensures that the waiting code does not exit early before all coroutines have finished executing. Use WaitGroup to avoid the end of the program before the coroutine execution is completed.
Next we will use a specific example to demonstrate the concurrent execution of tasks and the usage of Go WaitGroup.
First, we create a task function to simulate some time-consuming operations. This function will receive a task number as a parameter and print out the task number and the message of completion of execution:
func simulateTask(taskNum int) { time.Sleep(time.Duration(rand.Intn(3)) * time.Second) fmt.Printf("Task %d is complete ", taskNum) }
Next, we create a main function, create a set of tasks in this function, and use Go WaitGroup to wait for the completion of all tasks:
func main() { var wg sync.WaitGroup // 创建一个WaitGroup tasks := 10 // 设定任务数量 for i := 0; i < tasks; i++ { wg.Add(1) // 增加WaitGroup的计数器 go func(taskNum int) { defer wg.Done() // 减少WaitGroup的计数器 simulateTask(taskNum) // 执行任务 }(i) } wg.Wait() // 等待所有任务完成 fmt.Println("All tasks are completed") }
In this example, we first create a WaitGroup object and then set the number of tasks to be executed. In the task loop, we use wg.Add(1) to increase the WaitGroup counter, indicating that there is a task that needs to wait for completion. We then created an anonymous function using the go keyword where we performed the actual task and called wg.Done() to decrement the WaitGroup's counter when the task was completed. Finally, wait for all tasks to complete through the wg.Wait() statement.
By running this program, we can see the concurrent execution of tasks and the effect of Go WaitGroup. Each task will wait for a random period of time to complete execution and print out the corresponding completion message. Finally, when all tasks are completed, the program prints out the message "All tasks are completed".
This example shows the concurrent execution of tasks in Golang and the usage of Go WaitGroup. By rationally using concurrency and waiting mechanisms, program efficiency and performance can be improved. Using Golang's powerful concurrency features, we can write efficient and scalable concurrent programs.
To sum up, the concurrent execution of tasks in Golang and Go WaitGroup are important tools for achieving concurrency. By rationally using concurrency mechanisms, we can make full use of the capabilities of multi-core processors and improve program performance and responsiveness. At the same time, through the waiting function of WaitGroup, we can ensure that the program will not exit early before all tasks are completed. Go WaitGroup is a very useful tool for situations where you need to perform a large number of tasks and need to wait for them all to complete.
I hope that through this example, readers can understand the basic concepts and usage of concurrent task execution and Go WaitGroup, and flexibly apply it in actual projects.
The above is the detailed content of Concurrent task execution in Golang and Go WaitGroup. For more information, please follow other related articles on the PHP Chinese website!