关于Go语言中channel的问题。

刚开始学习Go,看到gorountine和channel的时候试了试下面这个例子:

package main
import (
    "fmt"
)

func display(msg string, c chan bool) {
    fmt.Println("display first message:", msg)
    c <- true
}

func main() {
    c := make(chan bool)
    go display("hello", c)
    go display("world", c)
    <-c
}

这样的话会输出两行结果:

display first message: hello
display first message: world

但是我总觉得应该只输出一行结果呢?main当中我定义了一个channel,之后分支出去两个gorountine去执行display函数,最后一行<-c阻塞等待数据。那么其中一个gorountine在打印完结果并向c发送数据后,main就接受到了数据,就应该继续执行然后结束了呀。怎么会打出两行结果呢?是不是我哪里理解的有问题?求指导,谢谢!

阅读 8.4k
3 个回答

From goroutine scheduler

go runtime and go scheduler The runtime keeps track of each goroutine, and will schedule them to run in turn on a pool of threads belonging to the process. Goroutines are separate from threads but rely upon them to run, and scheduling goroutines onto threads effectively is crucial for the efficient performance of Go programs. The idea behind goroutines is that they are capable of running concurrently, like threads, but are also extremely lightweight in comparison. So, while there might be multiple threads created for a process running a Go program, the ratio of goroutines to threads should be much higher than 1-to-1. Multiple threads are often necessary to ensure that goroutines are not unnecessarily blocked. When one goroutine makes a blocking call, the thread running it must block. Therefore, at least one more thread should be created by the runtime to continue the execution of other goroutines that are not in blocking calls. Multiple threads are allowed to run in parallel up to a programmer defined maximum, which is stored in the variable GOMAXPROCS

From understanding goroutines

Independently of cores and GOMAXPROCS, you can give the goroutine scheduler in the runtime a chance to do it's job. The scheduler cannot preempt a running goroutine but must wait for it to come back to the runtime and request some service, such as IO, time.Sleep(), or runtime.Gosched().

也就是说如果没有发生IO等需要挂起和谦让执行机会的时候,goroutine是不会终止的,除非执行完毕。

以我的理解来说明下这个问题。 主要问题是,这里虽然看似是开启了并发的模式,但实际上是单线程的,因为你没有设置GOMAXPROCS(默认是1,如果环境变量GOMAXPROCS也没有设置的话,该数值包含main thread),而且这些简单函数都不会导致阻塞操作,于是这样这些函数就是顺序执行的,没有并发效果。我们把你的例子修改一下改成下面这个

package main
import (
    "fmt"
)

func display1(msg string, c chan bool) {
    fmt.Println("display first message:", msg)
    c <- true
}


func display2(msg string, c chan bool) {
    fmt.Println("display second message:", msg)
    c <- true
}

func main() {
    c := make(chan bool)
    go display1("hello", c)
    go display2("world", c)
    <-c 
}

上面这个运行的结果还是会产生两个语句,但是你会发现即使把display2的chan操作注释掉这里依然会输出两次,和chan的是否阻塞是没有关系的,而且没有发生需要挂起的操作。 我们修改成第二个示例

package main
import (
    "fmt"
    "time"
)

func display1(msg string, c chan bool) {
    fmt.Println("display first message:", msg)
    c <- true
}


func display2(msg string, c chan bool) {
    time.Sleep(10e9)
    fmt.Println("display second message:", msg)
    c <- true
}

func main() {
    c := make(chan bool)
    go display1("hello", c)
    go display2("world", c)
    <-c 
}

这个时候的输出就只有display1了,这是因为我们的goroutine发现display2可以挂起,于是交出CPU(此时两个goroutine是执行在同一个底层thread里面的),这时候程序继续执行顺利结束。 那如果我们的display2不愿意交出CPU呢?比如下面,我们在display2上绑定了一个CPU密集型的操作,这个时候我们的程序就会一直等,等到display2执行完才会结束,因为它无法交出CPU。

package main
import (
    "fmt"
    "time"
)

func display1(msg string, c chan bool) {
    fmt.Println("display first message:", msg)
    c <- true
}


func display2(msg string, c chan bool) {
    for i:=0; i<100000000; i+=1{}
    fmt.Println("display second message:", msg)
    c <- true
}

func main() {
    c := make(chan bool)
    go display1("hello", c)
    go display2("world", c)
    <-c 
}

那我们不禁要问了那goroutine的优势在什么地方呢,如果它不能解决CPU的正常让出的话。首先要明确一个问题就是GO中影响groutine的重要参数GOMAXPROCS,系统默认是1,它决定的是底层可以用来运行goroutine的最大thread数目,这就是这里无法让出的原因,so,如果我们改成GOMAXPROCS大于1,上面的问题就解决了,因为我们的goroutine现在运行在两个不同的thread里面,如下,我们的程序不会等待display2就可以顺利结束。

package main
import (
    "fmt"
    "time"
    "runtime"
)

func display1(msg string, c chan bool) {
    fmt.Println("display first message:", msg)
    c <- true
}


func display2(msg string, c chan bool) {
    for i:=0; i<100000000; i+=1{}
    fmt.Println("display second message:", msg)
    c <- true
}

func main() {
    runtime.GOMAXPROCS(2)
    c := make(chan bool)
    go display1("hello", c)
    go display2("world", c)
    <-c 
}

程序只需要做一点点小小的改动

package main
import (
    "fmt"
)

func display(msg string, c chan bool) {
    // 交换这2行的位置
    c <- true
    fmt.Println("display first message:", msg)        
}

func main() {
    c := make(chan bool)
    go display("hello", c)
    go display("world", c)
    <-c
}

原因就是你的源程序是先输出在锁定,当然两个都会显示出来, 如果你先锁定在输出,只有当你使用了<-c的时候才会有一个被解锁输出出来显示

两个routine都会打印出结果,但只有第一个routine返回的结果会被main接收到。如果main中没有<-c的话,则第二个routine则会被卡在c->true里,除非chan声明的时候是带缓冲区的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题