go利用goroutine等待的取出元素的时机

利用goroutine等待的取出元素的时机,没有元素取出来,就一直阻塞,等待goroutine协程完成,取出队列中的元素,该进程才结束

点击查看代码
package main

import (
	"fmt"
	"time"
)

func hello(c chan bool) {
	time.Sleep(5 * time.Second)
	fmt.Println("hello goroutine")
	c <- true
}

func main() {
	var exitChan chan bool
	exitChan = make(chan bool)
	go hello(exitChan)
	fmt.Println("main thread terminate")
	<- exitChan // 利用goroutine等待的取出元素的时机,没有元素取出来,就一直阻塞,等待goroutine协程完成,取出队列中的元素,该进程才结束
}

posted @ 2022-03-10 21:14  ty1539  阅读(21)  评论(0编辑  收藏  举报