Skip to content

Instantly share code, notes, and snippets.

@ytxmobile98
Created December 18, 2024 05:51
Show Gist options
  • Select an option

  • Save ytxmobile98/dcf4abb7ac3ddcf0d48bb8fde25856fc to your computer and use it in GitHub Desktop.

Select an option

Save ytxmobile98/dcf4abb7ac3ddcf0d48bb8fde25856fc to your computer and use it in GitHub Desktop.
1000 Goroutines
package main
import (
"fmt"
"runtime"
"sync"
"time"
)
func main() {
const n int = 1000
const maxProcs int = 2
runtime.GOMAXPROCS(maxProcs)
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go func(i int) {
defer wg.Done()
time.Sleep(time.Duration(i) * time.Millisecond)
fmt.Printf("goroutine #%d, num goroutines: %d\n", i, runtime.NumGoroutine())
}(i)
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment