Home >Backend Development >Golang >How to Maintain a Constant Number of Simultaneously Running Goroutines in Go?

How to Maintain a Constant Number of Simultaneously Running Goroutines in Go?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-02 00:49:14910browse

How to Maintain a Constant Number of Simultaneously Running Goroutines in Go?

Threading Goroutines in Go: Running a Constant Number Simultaneously

The realm of Go concurrency offers a plethora of materials on awaiting the completion of a specified number of goroutines. However, a distinct challenge presents itself: ensuring a continuous execution of a predefined number of goroutines, with one commencing as another concludes.

Consider a scenario with a substantial quantity of tasks, such as processing data retrieved from a MySQL database. A naive approach might initiate a vast number of parallel goroutines, simultaneously executing hundreds of thousands of tasks. In contrast, the desired behavior limits the concurrently running goroutines to a fixed count (say, 20).

This controlled concurrency pattern is known as "bounded parallelism." Implementing this pattern in Go entails employing a channel of empty structs as a semaphore, dictating the maximum number of concurrent worker goroutines. Here's an illustration:

package main

import "fmt"

func main() {
    maxGoroutines := 10
    guard := make(chan struct{}, maxGoroutines)

    for i := 0; i < 30; i++ {
        guard <- struct{}{} // blocks if guard channel is full
        go func(n int) {
            worker(n)
            <-guard // unlocks a slot in guard
        }(i)
    }
}

func worker(i int) { fmt.Println("doing work on", i) }

This implementation ensures that no more than the specified number of goroutines execute concurrently. As a result, when a worker goroutine finishes, a new goroutine is immediately launched, maintaining the desired concurrency level.

The "Go Concurrency Patterns" article further explores this concept in its "Bounded parallelism" section, providing deeper insights into this crucial concurrency technique.

The above is the detailed content of How to Maintain a Constant Number of Simultaneously Running Goroutines in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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