Junior — Middle
Kas gorutinide loodud arv on seotud protsessori tuumade arvuga?
sobes.tech AI
Vastus AI-lt
The number of goroutines created in Go is not directly related to the number of CPU cores. You can create many goroutines — they are lightweight threads managed by the Go runtime, and their count is mainly limited by available memory and program logic.
The number of cores affects the number of OS threads that can run simultaneously, and the GOMAXPROCS parameter, which sets the maximum number of threads executing goroutines in parallel. But the number of goroutines usually far exceeds the number of cores, as they are efficiently scheduled and switched within the runtime.
Example:
runtime.GOMAXPROCS(4) // Use 4 cores
for i := 0; i < 1000; i++ {
go func(n int) {
fmt.Println("Goroutine", n)
}(i)
}
time.Sleep(time.Second)
Here, 1000 goroutines are created, even though only 4 cores are available.