在这方面,JAVA确实细致,
至于GO能否在线程池方法有JAVA的优势,
要时间证明。
package main
import (
"fmt"
"runtime"
"sync"
)
var (
counter int
wg sync.WaitGroup
mutex sync.Mutex
)
func main() {
runtime.GOMAXPROCS(1)
wg.Add(2)
fmt.Println("Start Goroutines")
go incCounter(10)
go incCounter(20)
fmt.Println("Waiting to Finish")
wg.Wait()
fmt.Println("\nFinal Counter: ", counter)
}
func incCounter(id int) {
defer wg.Done()
for count := 0; count < id; count++ {
mutex.Lock()
{
value := counter
runtime.Gosched()
value++
counter = value
}
mutex.Unlock()
}
}