热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

[Golang]Coroutine可能存在的死锁

直接上代码:1.第一种情况,如果没有select{},main主线程不会等待coroutine运行,导致coroutine得不到机会运行。Y


直接上代码:

1. 第一种情况, 如果没有select{}, main 主线程不会等待coroutine运行,导致coroutine得不到机会运行。

You are requesting eventual scheduling (using the two go statements) 
of two goroutines and then you exit main without giving the scheduler 
a chance to do anything. 

有了select, 程序正常运行。

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5.         "time"  
  6. )  
  7.   
  8.   
  9. func main() {  
  10.     go func1()  
  11.     go func2()  
  12.     select{}  
  13. }  
  14.   
  15. func func1() {  
  16.        for{  
  17.         fmt.Println("here1")  
  18.             time.Sleep(10 * time.Minute)  
  19.         }  
  20.   
  21. }  
  22.   
  23. func func2() {  
  24.        for{  
  25.        fmt.Println("here2")  
  26.            time.Sleep(10 * time.Minute)  
  27.        }  
package main
import (
"fmt"
"time"
)


func main() {
go func1()
go func2()
select{}
}

func func1() {
for{
fmt.Println("here1")
time.Sleep(10 * time.Minute)
}

}

func func2() {
for{
fmt.Println("here2")
time.Sleep(10 * time.Minute)
}
}

2. coroutine有机会运行,但是会发生死锁, fatal error: all goroutines are asleep - deadlock!

The goroutine executing func1 exited, ditto for func2. The main goroutine is blocked with no hope to recover while no other goroutine can be scheduled.

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5.     //"time"  
  6. )  
  7.   
  8. func main() {  
  9.     go func1()  
  10.     go func2()  
  11.     select {  
  12.     }  
  13. }  
  14.   
  15. func func1() {  
  16.     fmt.Println("here1")  
  17.   
  18. }  
  19.   
  20. func func2() {  
  21.     fmt.Println("here2")  
  22. }  
package mainimport ("fmt"//"time")func main() {go func1()go func2()select {}}func func1() {fmt.Println("here1")}func func2() {fmt.Println("here2")}


3. 第三种情况, 正常运行。

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5. )  
  6.   
  7. var c = make(chan int, 2)  
  8.   
  9. func main() {  
  10.     go func1()  
  11.     go func2()  
  12.     <-c  
  13.     <-c  
  14.     fmt.Println("ok")  
  15. }  
  16.   
  17. func func1() {  
  18.     fmt.Println("here1")  
  19.     c <- 1  
  20. }  
  21.   
  22. func func2() {  
  23.     fmt.Println("here2")  
  24.     c <- 1  
  25. }  
package mainimport ("fmt")var c = make(chan int, 2)func main() {go func1()go func2()<-c<-cfmt.Println("ok")}func func1() {fmt.Println("here1")c <- 1}func func2() {fmt.Println("here2")c <- 1}

4. 实现上面的目的的另外一种方法:

[plain] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. var wg sync.WaitGroup  
  2.   var urls = []string{  
  3.           "http://www.golang.org/",  
  4.           "http://www.google.com/",  
  5.           "http://www.somestupidname.com/",  
  6.   }  
  7.   for _, url := range urls {  
  8.           // Increment the WaitGroup counter.  
  9.           wg.Add(1)  
  10.           // Launch a goroutine to fetch the URL.  
  11.           go func(url string) {  
  12.                   // Decrement the counter when the goroutine completes.  
  13.                   defer wg.Done()  
  14.                   // Fetch the URL.  
  15.                   http.Get(url)  
  16.           }(url)  
  17.   }  
  18.   // Wait for all HTTP fetches to complete.  
  19.   wg.Wait()  


推荐阅读
author-avatar
同步管理精英
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有