溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程

發(fā)布時(shí)間:2023-04-18 16:12:33 來(lái)源:億速云 閱讀:87 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程”吧!

序文

select 是用來(lái)配合channel使用的

空select

  • 沒(méi)有內(nèi)容的select 會(huì)阻塞

  • 沒(méi)有內(nèi)容是指,沒(méi)有case,也沒(méi)有default

  • 如果沒(méi)有其它的任務(wù)指執(zhí)行,將會(huì)觸發(fā)死鎖

package main
import (
    "fmt"
    "time"
)
/**
* 沒(méi)有內(nèi)容的select 會(huì)阻塞
* 沒(méi)有內(nèi)容是指,沒(méi)有case,也沒(méi)有default
* 如果沒(méi)有其它的任務(wù)指執(zhí)行,將會(huì)觸發(fā)死鎖
*/
func main() {
    go func() {
        time.Sleep(2 * time.Second)
        fmt.Println("沒(méi)有內(nèi)容的select 會(huì)阻塞")
    }()
    select {}
}

怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程

只有default的select

只有default的select 和串行化沒(méi)有區(qū)別

package main
import "fmt"
/**
 只有default的select 和串行化沒(méi)有區(qū)別
 */
func main() {
	go func() {
		fmt.Println("quick")
	}()
	select {
	default:
		fmt.Println("end")
	}
}

怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程

帶 case 的 select

有case,有 default

  • 如果能匹配到case 就 執(zhí)行 case

  • 匹配不到case,就執(zhí)行default

  • 有 default,就代表了不會(huì)阻塞

package main
import (
	"fmt"
)
func main() {
	ch2 := make(chan int, 2)
	ch3 := make(chan int, 2)
	select {
	case v1 := <-ch2:
		fmt.Println(v1)
	case v2 := <-ch3:
		fmt.Println(v2)
	default:
		fmt.Println(22)
	}
}

怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程

package main
import (
	"fmt"
	"time"
)
func main() {
	ch2 := make(chan int, 2)
	ch3 := make(chan int, 2)
	go func() {
		ch2 <- 1
	}()
	time.Sleep(1 * time.Second)
	select {
	case v1 := <-ch2:
		fmt.Printf("get v1 chan value %d", v1)
	case v2 := <-ch3:
		fmt.Printf("get v1 chan value %d", v2)
	default:
		fmt.Println(22)
	}
}

怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程

有case,無(wú)default

會(huì)阻塞 一直等到case匹配上

package main
import (
	"fmt"
	"time"
)
func main() {
	ch2 := make(chan int, 2)
	ch3 := make(chan int, 2)
	fmt.Printf("start unix:%d \n", time.Now().Unix())
	go func() {
		time.Sleep(3 * time.Second)
		ch2 <- 1
	}()
	select {
	case v1 := <-ch2:
		fmt.Printf("case unix: %d \n", time.Now().Unix())
		fmt.Printf("get v1 chan value %d \n", v1)
	case v2 := <-ch3:
		fmt.Printf("get v1 chan value %d \n", v2)
	}
	fmt.Println("end")
}

怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程

select 只匹配一次,如果要進(jìn)行 n > 1 的 匹配,使用 for + select

package main
import (
	"fmt"
	"time"
)
func main() {
	ch2 := make(chan int, 2)
	ch3 := make(chan int, 2)
	fmt.Printf("start unix:%d \n", time.Now().Unix())
	go func() {
		for {
			time.Sleep(1 * time.Second)
			ch2 <- 1
		}
	}()
	for {
		select {
		case v1 := <-ch2:
			fmt.Printf("case unix: %d \n", time.Now().Unix())
			fmt.Printf("get v1 chan value %d \n", v1)
		case v2 := <-ch3:
			fmt.Printf("get v1 chan value %d \n", v2)
		}
	}
}

怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程

匹配是無(wú)序的

package main
import (
	"fmt"
	"time"
)
func test() {
	ch2 := make(chan int)
	ch3 := make(chan int)
	go func() {
		ch2 <- 1
		close(ch2)
	}()
	go func() {
		time.Sleep(1 * time.Second)
		ch3 <- 1
		close(ch3)
	}()
	time.Sleep(2 * time.Second)
	// 如果有順序,那么因該每次都是v1
	select {
	case v1 := <-ch2:
		fmt.Printf("case unix: %d \n", time.Now().Unix())
		fmt.Printf("get v1 chan value %d \n", v1)
	case v2 := <-ch3:
		fmt.Printf("case unix: %d \n", time.Now().Unix())
		fmt.Printf("get v2 chan value %d \n", v2)
	}
}
func main() {
	for i := 0; i < 10; i++ {
		test()
	}
}

怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程

感謝各位的閱讀,以上就是“怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)怎么使用Golang中的select語(yǔ)句實(shí)現(xiàn)并發(fā)編程這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI