Use Go WaitGroup to implement efficient concurrent processing tasks

王林
Release: 2023-09-27 15:01:13
Original
1010 people have browsed it

使用Go WaitGroup实现高效并发处理任务

Use Go WaitGroup to achieve efficient concurrent processing tasks

In the Go language, use WaitGroup to achieve efficient concurrent processing tasks. WaitGroup is a counting semaphore used to wait for a group of goroutines to complete their work before continuing to the next step.

WaitGroup has three main methods: Add(), Done() and Wait(). The Add() method is used to increase the number of waiting goroutines, the Done() method indicates that a goroutine has completed, and the Wait() method is used to block the main goroutine until all goroutines have completed.

The following will use a simple example to illustrate how to use WaitGroup to process tasks concurrently.

First, we need to create a WaitGroup instance:

var wg sync.WaitGroup
Copy after login

Then, we can use the Add() method to increase the number of waiting goroutines, and call the Done() method at the end of each goroutine:

func main() {
    // 设置等待的goroutine数量
    wg.Add(2)

    // 启动第一个goroutine
    go doTask1()

    // 启动第二个goroutine
    go doTask2()

    // 等待所有goroutine完成
    wg.Wait()
}

func doTask1() {
    // 模拟任务1
    time.Sleep(time.Second * 1)
    fmt.Println("Task 1 is done!")

    // 调用Done()方法表示任务完成
    wg.Done()
}

func doTask2() {
    // 模拟任务2
    time.Sleep(time.Second * 2)
    fmt.Println("Task 2 is done!")

    // 调用Done()方法表示任务完成
    wg.Done()
}
Copy after login

In the above example, we created two goroutines to execute the doTask1() and doTask2() functions. Each function will simulate a time-consuming task. After starting all goroutines, the main goroutine calls the Wait() method to block itself until all tasks are completed.

Run the above code, you can get output similar to the following:

Task 1 is done!
Task 2 is done!
Copy after login

You can see that the two tasks are executed in parallel, and the corresponding information is printed out after completion. Use WaitGroup to facilitate concurrent processing and resume the execution of the main goroutine after all tasks are completed.

It should be noted that when using WaitGroup, you must ensure that each task calls the Done() method, otherwise the main goroutine will always be blocked at the Wait() method, causing the program to be unable to continue execution.

To sum up, Go WaitGroup is a simple and effective concurrency control tool that can help us achieve efficient concurrent processing tasks. By calling the Add(), Done(), and Wait() methods, we can control the execution order of goroutines and continue to perform subsequent operations after all tasks are completed.

I hope this article will help you understand concurrent processing tasks and use Go WaitGroup!

The above is the detailed content of Use Go WaitGroup to implement efficient concurrent processing tasks. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template