1 回答

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
好的,讓我們從這個(gè)工作示例開(kāi)始:
func Test_t(t *testing.T) {
// just a published, this publishes result on a chan
publish := func(s int, ch chan int, wg *sync.WaitGroup) {
ch <- s // this is blocking!!!
wg.Done()
}
wg := &sync.WaitGroup{}
wg.Add(100)
// we'll use done channel to notify the work is done
res := make(chan int)
done := make(chan struct{})
// create worker that will notify that all results were published
go func() {
wg.Wait()
done <- struct{}{}
}()
// let's create a jobs that publish on our res chan
// please note all goroutines are created immediately
for i := 0; i < 100; i++ {
go publish(i, res, wg)
}
// lets get 30 args and then wait
var resCounter int
forloop:
for {
select {
case ss := <-res:
println(ss)
resCounter += 1
// break the loop
if resCounter%30 == 0 {
// after receiving 30 results we are blocking this thread
// no more results will be taken from the channel for 5 seconds
println("received 30 results, waiting...")
time.Sleep(5 * time.Second)
}
case <-done:
// we are done here, let's break this infinite loop
break forloop
}
}
}
我希望這進(jìn)一步表明它是如何完成的。
那么,您的代碼有什么問(wèn)題?老實(shí)說(shuō),它看起來(lái)不錯(cuò)(我的意思是發(fā)布了 30 個(gè)結(jié)果,然后代碼等待,然后是另外 30 個(gè)結(jié)果,等等),但問(wèn)題是您想在哪里等待?
我猜有幾種可能:
創(chuàng)建工人(這就是您的代碼現(xiàn)在的工作方式,正如我所見(jiàn),它以 30 個(gè)包發(fā)布作業(yè);請(qǐng)注意,您在
digit
函數(shù)中的 2 秒延遲僅適用于執(zhí)行代碼的 goroutine)觸發(fā)工人(所以“等待”代碼應(yīng)該在工人函數(shù)中,不允許運(yùn)行更多的工人 - 所以它必須觀察發(fā)布了多少結(jié)果)
處理結(jié)果(這就是我的代碼的工作方式,并且正確的同步在
forloop
)
- 1 回答
- 0 關(guān)注
- 124 瀏覽
添加回答
舉報(bào)