重写多协程问题
This commit is contained in:
25
test/Producer.go
Normal file
25
test/Producer.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GoProducer(mx *sync.Mutex, ch chan int) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// 生成 [0,100) 随机整数
|
||||
randomInt := rand.Intn(100)
|
||||
timech := time.Tick(time.Second)
|
||||
defer mx.Unlock()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-timech:
|
||||
ch <- randomInt
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user