欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

掌握Golang中的select語句實(shí)現(xiàn)并發(fā)編程

 更新時(shí)間:2023年04月18日 10:02:58   作者:西直門三太子  
Golang中的select語句用于在多個(gè)通道間選擇可讀或可寫的操作,并阻塞等待其中一個(gè)通道進(jìn)行操作。可以用于實(shí)現(xiàn)超時(shí)控制、取消和中斷操作等。同時(shí),select語句支持default分支,用于在沒有任何通道可操作時(shí)執(zhí)行默認(rèn)操作

序文

select 是用來配合channel使用的

空select

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

只有default的select

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

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

帶 case 的 select

有case,有 default

  • 如果能匹配到case 就 執(zhí)行 case
  • 匹配不到case,就執(zhí)行default
  • 有 default,就代表了不會(huì)阻塞
package main
import (
	"fmt"
)
func main() {
	ch1 := make(chan int, 2)
	ch2 := make(chan int, 2)
	select {
	case v1 := <-ch1:
		fmt.Println(v1)
	case v2 := <-ch2:
		fmt.Println(v2)
	default:
		fmt.Println(22)
	}
}

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

有case,無default

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

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

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

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

匹配是無序的

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

到此這篇關(guān)于掌握Golang中的select語句實(shí)現(xiàn)并發(fā)編程的文章就介紹到這了,更多相關(guān)Golang中的select語句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論