如何使用 Channels 让 goroutines 相互通信
How to make goroutines talk to each other using Channels
我是 Golang 的新手,我正在尝试使用 goroutines 以便它们之间可以交谈。我有一些代码可以启动一个具有 operation1 的 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 作为第二个参数,所有 goroutines 都睡着了。
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")
}
}()
}
非常感谢对代码的任何更正以及使用此方法的陷阱。
在无缓冲通道的情况下(未指定大小时)它不能保存值,因为它没有大小。因此 reader 必须在 writing/transmiting 数据通过通道时出现,否则它将阻塞调用。
func main() {
startDance := make(chan bool)
startDance <- true
}
但是当您在上面的代码中指定一个大小(比如 1)时,它不会成为死锁,因为它将 space 来保存数据。 ((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_
我是 Golang 的新手,我正在尝试使用 goroutines 以便它们之间可以交谈。我有一些代码可以启动一个具有 operation1 的 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 作为第二个参数,所有 goroutines 都睡着了。
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")
}
}()
}
非常感谢对代码的任何更正以及使用此方法的陷阱。
在无缓冲通道的情况下(未指定大小时)它不能保存值,因为它没有大小。因此 reader 必须在 writing/transmiting 数据通过通道时出现,否则它将阻塞调用。
func main() {
startDance := make(chan bool)
startDance <- true
}
但是当您在上面的代码中指定一个大小(比如 1)时,它不会成为死锁,因为它将 space 来保存数据。 ((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_