婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識(shí)庫 > Golang通道的無阻塞讀寫的方法示例

Golang通道的無阻塞讀寫的方法示例

熱門標(biāo)簽:中國地圖標(biāo)注省會(huì)高清 西部云谷一期地圖標(biāo)注 南通如皋申請(qǐng)開通400電話 江西轉(zhuǎn)化率高的羿智云外呼系統(tǒng) 浙江高速公路地圖標(biāo)注 學(xué)海導(dǎo)航地圖標(biāo)注 地圖標(biāo)注的汽車標(biāo) 廣州呼叫中心外呼系統(tǒng) 高德地圖標(biāo)注口訣

無論是無緩沖通道,還是有緩沖通道,都存在阻塞的情況,但其實(shí)有些情況,我們并不想讀數(shù)據(jù)或者寫數(shù)據(jù)阻塞在那里,有1個(gè)唯一的解決辦法,那就是使用select結(jié)構(gòu)。

這篇文章會(huì)介紹,哪些情況會(huì)存在阻塞,以及如何使用select解決阻塞。

阻塞場景

阻塞場景共4個(gè),有緩存和無緩沖各2個(gè)。

無緩沖通道的特點(diǎn)是,發(fā)送的數(shù)據(jù)需要被讀取后,發(fā)送才會(huì)完成,它阻塞場景:

  1. 通道中無數(shù)據(jù),但執(zhí)行讀通道。
  2. 通道中無數(shù)據(jù),向通道寫數(shù)據(jù),但無協(xié)程讀取。
// 場景1
func ReadNoDataFromNoBufCh() {
 noBufCh := make(chan int)

 -noBufCh
 fmt.Println("read from no buffer channel success")

 // Output:
 // fatal error: all goroutines are asleep - deadlock!
}

// 場景2
func WriteNoBufCh() {
 ch := make(chan int)

 ch - 1
 fmt.Println("write success no block")
 
 // Output:
 // fatal error: all goroutines are asleep - deadlock!
}

注:示例代碼中的Output注釋代表函數(shù)的執(zhí)行結(jié)果,每一個(gè)函數(shù)都由于阻塞在通道操作而無法繼續(xù)向下執(zhí)行,最后報(bào)了死鎖錯(cuò)誤。

有緩存通道的特點(diǎn)是,有緩存時(shí)可以向通道中寫入數(shù)據(jù)后直接返回,緩存中有數(shù)據(jù)時(shí)可以從通道中讀到數(shù)據(jù)直接返回,這時(shí)有緩存通道是不會(huì)阻塞的,它阻塞的場景是:

  1. 通道的緩存無數(shù)據(jù),但執(zhí)行讀通道。
  2. 通道的緩存已經(jīng)占滿,向通道寫數(shù)據(jù),但無協(xié)程讀。
// 場景1
func ReadNoDataFromBufCh() {
 bufCh := make(chan int, 1)

 -bufCh
 fmt.Println("read from no buffer channel success")

 // Output:
 // fatal error: all goroutines are asleep - deadlock!
}

// 場景2
func WriteBufChButFull() {
 ch := make(chan int, 1)
 // make ch full
 ch - 100

 ch - 1
 fmt.Println("write success no block")
 
 // Output:
 // fatal error: all goroutines are asleep - deadlock!
}

使用Select實(shí)現(xiàn)無阻塞讀寫

select是執(zhí)行選擇操作的一個(gè)結(jié)構(gòu),它里面有一組case語句,它會(huì)執(zhí)行其中無阻塞的那一個(gè),如果都阻塞了,那就等待其中一個(gè)不阻塞,進(jìn)而繼續(xù)執(zhí)行,它有一個(gè)default語句,該語句是永遠(yuǎn)不會(huì)阻塞的,我們可以借助它實(shí)現(xiàn)無阻塞的操作。

下面示例代碼是使用select修改后的無緩沖通道和有緩沖通道的讀寫,以下函數(shù)可以直接通過main函數(shù)調(diào)用,其中的Ouput的注釋是運(yùn)行結(jié)果,從結(jié)果能看出,在通道不可讀或者不可寫的時(shí)候,不再阻塞等待,而是直接返回。

// 無緩沖通道讀
func ReadNoDataFromNoBufChWithSelect() {
 bufCh := make(chan int)

 if v, err := ReadWithSelect(bufCh); err != nil {
  fmt.Println(err)
 } else {
  fmt.Printf("read: %d\n", v)
 }

 // Output:
 // channel has no data
}

// 有緩沖通道讀
func ReadNoDataFromBufChWithSelect() {
 bufCh := make(chan int, 1)

 if v, err := ReadWithSelect(bufCh); err != nil {
  fmt.Println(err)
 } else {
  fmt.Printf("read: %d\n", v)
 }

 // Output:
 // channel has no data
}

// select結(jié)構(gòu)實(shí)現(xiàn)通道讀
func ReadWithSelect(ch chan int) (x int, err error) {
 select {
 case x = -ch:
  return x, nil
 default:
  return 0, errors.New("channel has no data")
 }
}

// 無緩沖通道寫
func WriteNoBufChWithSelect() {
 ch := make(chan int)
 if err := WriteChWithSelect(ch); err != nil {
  fmt.Println(err)
 } else {
  fmt.Println("write success")
 }

 // Output:
 // channel blocked, can not write
}

// 有緩沖通道寫
func WriteBufChButFullWithSelect() {
 ch := make(chan int, 1)
 // make ch full
 ch - 100
 if err := WriteChWithSelect(ch); err != nil {
  fmt.Println(err)
 } else {
  fmt.Println("write success")
 }

 // Output:
 // channel blocked, can not write
}

// select結(jié)構(gòu)實(shí)現(xiàn)通道寫
func WriteChWithSelect(ch chan int) error {
 select {
 case ch - 1:
  return nil
 default:
  return errors.New("channel blocked, can not write")
 }
}

使用Select+超時(shí)改善無阻塞讀寫

使用default實(shí)現(xiàn)的無阻塞通道阻塞有一個(gè)缺陷:當(dāng)通道不可讀或?qū)懙臅r(shí)候,會(huì)即可返回。實(shí)際場景,更多的需求是,我們希望,嘗試讀一會(huì)數(shù)據(jù),或者嘗試寫一會(huì)數(shù)據(jù),如果實(shí)在沒法讀寫,再返回,程序繼續(xù)做其它的事情。

使用定時(shí)器替代default可以解決這個(gè)問題。比如,我給通道讀寫數(shù)據(jù)的容忍時(shí)間是500ms,如果依然無法讀寫,就即刻返回,修改一下會(huì)是這樣:

func ReadWithSelect(ch chan int) (x int, err error) {
 timeout := time.NewTimer(time.Microsecond * 500)

 select {
 case x = -ch:
  return x, nil
 case -timeout.C:
  return 0, errors.New("read time out")
 }
}

func WriteChWithSelect(ch chan int) error {
 timeout := time.NewTimer(time.Microsecond * 500)

 select {
 case ch - 1:
  return nil
 case -timeout.C:
  return errors.New("write time out")
 }
}

結(jié)果就會(huì)變成超時(shí)返回:

read time out
write time out
read time out
write time out

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:吐魯番 常州 曲靖 許昌 貴州 東營 德宏 保定

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Golang通道的無阻塞讀寫的方法示例》,本文關(guān)鍵詞  Golang,通道,的,無,阻塞,讀,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Golang通道的無阻塞讀寫的方法示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于Golang通道的無阻塞讀寫的方法示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 饶阳县| 西畴县| 黎平县| 龙海市| 仪征市| 西藏| 西充县| 汝南县| 天全县| 涟水县| 库车县| 邮箱| 达州市| 东莞市| 乌鲁木齐市| 丽江市| 临泉县| 宿迁市| 琼中| 霸州市| 敦化市| 西林县| 登封市| 壶关县| 台南县| 黑龙江省| 上虞市| 西盟| 宁晋县| 安庆市| 大埔区| 三明市| 思南县| 普兰店市| 灵武市| 廊坊市| 平和县| 长子县| 冀州市| 通许县| 河北区|