channel synchronization _ golang

we can use channels to sychronize execution across goroutines. Here's an example of using a blocking receive to to wait for a goroutine to finsh

package main

import (
    "fmt"
    "time"
)

func worker(done chan bool) {
    fmt.Println("working..")
    time.Sleep(time.Second)
    fmt.Println("done")
    done <- true
}

func main() {

    done := make(chan bool, 1)
    go worker(done)
    fmt.Println("done", <-done)
    fmt.Println(<-done)
}
working..
done
done true

总结 :

   1 : .......

posted on 2015-03-16 13:29  xjk112  阅读(198)  评论(0编辑  收藏  举报