package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int, 2)
defer close(c)
//提前將隊列放滿
c <- 1
c <- 2
fmt.Println("開始嘗試執行")
go cross(c)
process(c)
}
func process(c chan int) {
fmt.Println("被限流阻塞")
c <- 1 //channel已滿,將阻塞,直到成功放入channel **
fmt.Println("已放行,執行process")
}
func cross(c chan int) {
a := 1
b := 3
for b >= a {
fmt.Println("阻塞", b, "秒")
time.Sleep(time.Second)
b -= 1
}
fmt.Println("釋放一個通行證")
<-c //取出元素,則chan可以繼續放入數據,將喚醒**行代碼
}