如何使用 Channels 让 goroutine 相互通信
来源:stackoverflow
时间:2024-04-14 15:54:36 348浏览 收藏
欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《如何使用 Channels 让 goroutine 相互通信》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!
我是 golang 新手,正在尝试使用 goroutine,以便它们可以在它们之间进行通信。我有一些代码启动一个具有操作 1 的 goroutine,我称之为跳舞。当它完成时,它会向另一个 goroutine 发出信号,该 goroutine 执行另一个操作2,比如说睡眠。
你可以将一个force dance参数传递给dance goroutine,但如果它已经处于dance状态,它就会休眠。
package main import ( "fmt" "time" ) func main(){ test("notdancing", true) time.sleep(10*time.second) } func dance()error{ fmt.println("tapping my feet") time.sleep(10*time.second) return nil } func test(status string, forcedance bool) {
这在以下情况下不起作用
//startsleep := make(chan bool)
为什么通道需要提供缓冲区长度才能正常工作?我尝试不设置缓冲区长度,但它说如果我不传递 1 作为第二个参数,所有 goroutine 都会休眠。
startdance := make(chan bool, 1) startSleep := make(chan bool, 1) if status == "dancing" && forceDance { select { case startSleep <-true: fmt.Println("Would start to sleep now") default: fmt.Println("Sleep Already started. No need to force") } } if status != "dancing" { fmt.Println("Startingdance") startdance <- true } go func() { <-startdance err := dance() if err == nil { select { case startSleep <- true: fmt.Println("Starting Sleeping, dancing completed") default: fmt.Println("Already started Sleeping") } } else { fmt.Println("Not in a mood to dance today") } }() go func() { <-startSleep if forceDance { fmt.Println("Force sleep because forcing to dance while already dancing") } }() }
我非常感谢对代码的任何更正以及使用这种方法的陷阱。
解决方案
在无缓冲通道的情况下(未指定大小时),它无法保存值,因为它没有大小。因此,通过通道写入/传输数据时必须有读取器在场,否则将阻塞调用。
func main() { startdance := make(chan bool) startdance <- true }
但是,当您在上面的代码中指定大小(例如 1)时,就不会出现死锁,因为它将有空间来保存数据。 ((https://robertbasic.com/blog/buffered-vs-unbuffered-channels-in-golang/) .)(https://www.golang-book.com/books/intro/10) 您可以查看上述网站以更好地了解通道和并发
package main import ( "fmt" "time" ) func main() { startDance := make(chan bool) startSleep := make(chan bool) forceSleep := make(chan bool) go startDance1(startDance, forceSleep, startSleep) go performSleep(startSleep, startDance) startDance <- true fmt.Println("now dance is started ") forceSleep <- true select {} } func startDance1(startDance chan bool, forceSleep chan bool, startSleep chan bool) { fmt.Println("waiting to start dance") select { case <-startDance: fmt.Println("staring dance") } for { select { case <-startDance: fmt.Println("starting dance") case <-forceSleep: fmt.Println("aleardy dancing going to sleep") select { case startSleep <- true: default: } default: //this is just to show working this // i added default or else this will go into deadlock fmt.Println("dancing") time.Sleep(time.Second * 1) } } } func performSleep(startSleep chan bool, startDance chan bool) { select { case <-startSleep: fmt.Println("staring sleep") } fmt.Println("sleeping for 5 seconds ") time.Sleep(time.Second * 5) select { case startDance <- true: fmt.Println("started dance") default: fmt.Println("failed to start dance ") } }
上面的代码比您的代码稍有改进(我尝试根据您的要求进行制作)。我建议你读一些书来了解更多关于 go 并发的知识(https://www.golang-book.com/books/intro/10_
到这里,我们也就讲完了《如何使用 Channels 让 goroutine 相互通信》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习