最简单的,接下来,会是竞争,加锁。。。
package main import ( "fmt" "runtime" "sync" ) var wg sync.WaitGroup func main() { runtime.GOMAXPROCS(runtime.NumCPU()) wg.Add(2) fmt.Println("Create Goroutines") go printPrime("A") go printPrime("B") fmt.Println("Waiting To Finish") wg.Wait() fmt.Println("\nTerminating Program") } func printPrime(prefix string) { defer wg.Done() next: for outer :=2; outer < 5000; outer++ { for inner := 2; inner < outer; inner++ { if outer%inner == 0 { continue next } } fmt.Printf("%s:%d,", prefix, outer) } fmt.Println("Completed", prefix) }