第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

隨著時(shí)間超時(shí)。After 的行為不像用自動(dòng)收?qǐng)?bào)機(jī)或計(jì)時(shí)器超時(shí)

隨著時(shí)間超時(shí)。After 的行為不像用自動(dòng)收?qǐng)?bào)機(jī)或計(jì)時(shí)器超時(shí)

Go
ITMISS 2021-11-08 16:14:48
我希望以下功能的行為方式相同func fillChanTimeoutUsingTicker(maxDuration time.Duration, chanSize int) chan string {    c := make(chan string, chanSize)    ticker := time.NewTicker(maxDuration)    for {        select {        case <-ticker.C:            ticker.Stop()            fmt.Println("Ticker:operation timedout")            return c        case c <- "Random message":        default:            fmt.Println("Ticker:chan is full")            return c        }    }}func fillChanTimeoutUsingTimeAfter(maxDuration time.Duration, chanSize int) chan string {    c := make(chan string, chanSize)    for {        select {        case <-time.After(maxDuration):            fmt.Println("time.After:operation timedout")            return c        case c <- "Random message":        default:            fmt.Println("time.After:chan is full")            return c        }    }}稱他們?yōu)椋?nbsp;   resWithTicker := fillChanTimeoutUsingTicker(time.Duration(1*time.Microsecond), 10000000)    fmt.Println(len(resWithTicker))    resWithTimeAfter := fillChanTimeoutUsingTimeAfter(time.Duration(1*time.Microsecond), 10000000)    fmt.Println(len(resWithTimeAfter))印刷:Ticker:operation timedout43979time.After:chan is full10000000我認(rèn)為他們會(huì)以完全相同的方式行事,我真的沒有得到巨大的差異,對(duì)此有什么想法嗎?請(qǐng)注意,還可以像在股票代碼功能中一樣使用計(jì)時(shí)器按預(yù)期工作。
查看完整描述

1 回答

?
回首憶惘然

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊

問題出在您的代碼中。


在您的第一個(gè)示例中,您正在創(chuàng)建一個(gè)股票代碼并將其用于超時(shí)。

在您的第二個(gè)示例中,您每次循環(huán)時(shí)都會(huì)創(chuàng)建一個(gè)計(jì)時(shí)器:


case <-time.After(maxDuration):

從庫(kù)源中可以看出,這相當(dāng)于


case <- time.NewTimer(maxDuration).C:

如果每次循環(huán)時(shí)都創(chuàng)建一個(gè)新的 Ticker/Timer(并丟棄舊的),它可能永遠(yuǎn)不會(huì)觸發(fā)。


因此,為了讓您的第二個(gè)示例正確運(yùn)行,請(qǐng)這樣做(未經(jīng)測(cè)試):


func fillChanTimeoutUsingTimeAfter(maxDuration time.Duration, chanSize int) chan string {

    c := make(chan string, chanSize)

    t := time.After(maxDuration)

    for {

        select {

        case <-t:

            fmt.Println("time.After:operation timedout")

            return c

        case c <- "Random message":

        default:

            fmt.Println("time.After:chan is full")

            return c

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-11-08
  • 1 回答
  • 0 關(guān)注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)