問題描述兩個(gè)goroutine,一個(gè)goroutine中做鍵盤的輸入,另一個(gè)goroutine做其它數(shù)據(jù)處理。運(yùn)行后,如果我不輸入數(shù)據(jù),另一個(gè)goroutine也停止運(yùn)行了。也沒調(diào)度。我希望即使我不輸入數(shù)據(jù),也不要影響另一個(gè)goroutine。問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法相關(guān)代碼//請(qǐng)把代碼文本粘貼到下方(請(qǐng)勿用圖片代替代碼)packagemainimport("fmt""os""bufio""math/rand")funcmain(){msgChan:=make(chanstring)roundChan:=make(chanint)sleepChan:=make(chanbool)goinput(msgChan)goroundNum(roundChan)goScheduler(msgChan,roundChan)<-sleepChan}funcScheduler(msgChanchanstring,roundChanchanint){var(msgstringroundint)for{select{casemsg=<-msgChan:fmt.Println("msg:",msg)caseround=<-roundChan:fmt.Println(round)}}}funcroundNum(roundChanchanint){roundChan<-rand.Int()}funcinput(msgChanchanstring){var(msgstringerrerror)for{fmt.Println("請(qǐng)輸入要回復(fù)的消息:")ifmsg,err=bufio.NewReader(os.Stdin).ReadString('\n');err!=nil{return}msgChan<-msg}}你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?期待:可以隨時(shí)輸入鍵盤數(shù)據(jù),如果我輸入鍵盤數(shù)據(jù)兩個(gè)goroutine都正常運(yùn)行。如果我不輸入鍵盤數(shù)據(jù),也不要影響另一個(gè)goroutine的執(zhí)行。
急求!?。?!黨goroutine調(diào)度被阻塞怎么處理,怎么解決?怎么回事哈???
哆啦的時(shí)光機(jī)
2019-06-09 09:55:17