go 使用信号量判断多个协程处理完毕
package main
import (
"fmt"
"net/http"
"time"
)
var urls = []string{
"http://www.baidu.com/",
"http://www.clearloveuzi.cn/",
"http://www.sina.com/",
}
把上面那个给换了种方式判断协程处理完(信号量-the way to go)
func main() {
// Execute an HTTP HEAD request for all url's
// and returns the HTTP status string or an error string.
stime:=time.Now().UnixNano()
chan1:=make(chan bool)
for _, url := range urls {
//fmt.Println("aaaaa",url)
go func(url string) {
stime1:=time.Now().UnixNano()
resp, err := http.Head(url)
if err != nil {
fmt.Println("Error:", url, err)
}
etime1:=time.Now().UnixNano()
fmt.Println(url, ": ", resp.Status,"--",(etime1-stime1) / 1e6)
chan1<-true
//close(chan1)
}(url)
}
for i:=0;i<len(urls);i++{
_,ok:=<-chan1
fmt.Println(ok)
}
//<-chan1
//<-chan1
//<-chan1
//<-chan1//todo 多一个就不会退出程序了哦 会一直阻塞
//todo important 注意 ! 这里也不能用go协程单独运行哦 必须在主程序阻塞这个队列
// for i:=range chan1{ //todo range 会一直阻塞住
// fmt.Println(i)
//}
etime:=time.Now().UnixNano()
fmt.Println("allfuck",(etime-stime) / 1e6)
}
Tags : 本文未设置标签
您可以自由的转载和修改,但请务必注明文章来源并且不可用于商业目的。
本站大部分内容收集于互联网,如果有侵权内容、不妥之处,请联系删除。敬请谅解!
Previous post
round=回合 robin=知更鸟 roundrobin=轮询
Next post
一致性hash p站