Middle
How will the system behave when launching 10,000 threads simultaneously?
sobes.tech AI
Answer from AI
Running 10,000 goroutines in Go is possible and usually does not cause problems, as goroutines are very lightweight compared to system threads. The Go runtime efficiently manages their scheduling and distribution across system threads.
However, with such a number of goroutines, it is important to consider:
- Memory: each goroutine starts with a small stack (usually 2KB), but as the stack grows, memory consumption increases.
- Resource contention: if all goroutines are actively working and blocking on shared resources, performance degradation may occur.
- Go scheduler: it efficiently distributes goroutines, but at an extreme count, overhead may increase.
Example of running 10,000 goroutines:
for i := 0; i < 10000; i++ {
go func(id int) {
// some work
fmt.Println("Goroutine", id)
}(i)
}
// Wait for goroutines to finish, for example, using sync.WaitGroup
Overall, Go allows scaling the number of goroutines well above 10,000, but it is important to monitor memory consumption and blocking.