package main
import (
"log"
"math/rand"
"sync"
"time"
)
type A struct {
C *sync.Cond
I int
}
func main() {
c := &A{
C: sync.NewCond(&sync.Mutex{}),
I: 1,
}
for i := 1000; i > 0; i-- {
go gog(i, c)
}
c.C.L.Lock()
for c.I != 1001 {
c.C.Wait()
}
c.C.L.Unlock()
}
func gog(index int, c *A) {
c.C.L.Lock()
for c.I != index {
c.C.Wait()
}
//模拟任务
time.Sleep(time.Millisecond * time.Duration(rand.Intn(200)))
log.Println(index)
c.I = index + 1
c.C.L.Unlock()
c.C.Broadcast()
}