golang coroutine 的等待与死锁用法
来源:脚本之家
时间:2023-01-07 12:04:46 296浏览 收藏
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《golang coroutine 的等待与死锁用法》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~
直接上代码:
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, 程序正常运行。
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.
package main
import (
"fmt"
//"time"
)
func main() {
go func1()
go func2()
select {
}
}
func func1() {
fmt.Println("here1")
}
func func2() {
fmt.Println("here2")
}
3. 第三种情况, 正常运行
package main
import (
"fmt"
)
var c = make(chan int, 2)
func main() {
go func1()
go func2()
<h2>4. 实现上面的目的的另外一种方法:</h2>
<pre class="brush:plain;">
var wg sync.WaitGroup
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Increment the WaitGroup counter.
wg.Add(1)
// Launch a goroutine to fetch the URL.
go func(url string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
// Fetch the URL.
http.Get(url)
}(url)
}
// Wait for all HTTP fetches to complete.
wg.Wait()
补充:golang中死锁的情况分析
Golang关于channel死锁情况的汇总以及解决方案
直接读取空channel的死锁
当一个channel中没有数据,而直接读取时,会发生死锁:
func main() {
q := make(chan int, 2)
<p>错误提示:</p>
<blockquote>
<p>fatal error: all goroutines are asleep - deadlock!</p>
<p>goroutine 1 [chan receive]:</p>
<p>main.main()</p>
<p>/home/erick/Desktop/Book/Parallel/final.go:159 +0x4d</p>
<p>exit status 2</p>
</blockquote>
<p>上述代码中,创建了一个2个容量的channel,直接读取发生了死锁情况。</p>
<p>修正方案,使用select方法阻止,在default中放置默认处理方式:</p>
<pre class="brush:plain;">
func main() {
q := make(chan int, 2)
select {
case v :=
<p>输出:</p>
<blockquote>
<p>nothing in channel</p>
</blockquote>
<p>过度写入数据造成的死锁</p>
<p>写入数据超过channel的容量,也会造成死锁:</p>
<pre class="brush:plain;">
func main() {
q := make(chan int, 2)
q
<p>解决方案,与写入的方式一样,使用select方法阻止,在default中放置默认处理方式:</p>
<pre class="brush:plain;">
func main() {
q := make(chan int, 2)
q
<p>向已经关闭的channel中写入数据</p>
<p>这种造成的不是死锁,而是产生panic。</p>
<pre class="brush:plain;">
func main() {
q := make(chan int, 2)
close(q)
q
<p>代码错误:</p>
<blockquote>
<p>panic: send on closed channel</p>
<p>goroutine 1 [running]:</p>
<p>main.main()</p>
<p>/home/erick/Desktop/Book/Parallel/final.go:154 +0x63</p>
<p>exit status 2</p>
</blockquote>
<p>解决方案:只有别向已经关闭的channel写数据。。。。</p>
<p>但是,可以从已经关闭的channel中读取数据:</p>
<pre class="brush:plain;">
func main() {
q := make(chan int, 3)
q
<p>下面开始讲解goroutine的读写</p>
<pre class="brush:plain;">
package main
import "fmt"
func main() {
c:=make(chan string)
go func() {
c
<p>上面这个是对的</p>
<pre class="brush:plain;">
package main
import "fmt"
func main() {
c:=make(chan string)
fmt.Println(
<p>上面这个是错的,会发生死锁,因为程序会阻塞在fmt.Println(
</p><h2>总结:</h2>
<p>上述提到的死锁,是指在程序的主线程中发生的情况,如果上述的情况发生在非主线程中,读取或者写入的情况是发生堵塞的,而不是死锁。</p>
<p>实际上,阻塞情况省去了我们加锁的步骤,反而是更加有利于代码编写,要合理的利用阻塞。。</p>
<p>以上为个人经验,希望能给大家一个参考,也希望大家多多支持golang学习网。如有错误或未考虑完全的地方,望不吝赐教。</p><p>以上就是《golang coroutine 的等待与死锁用法》的详细内容,更多关于golang的资料请关注golang学习网公众号!</p>
声明:本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
462 收藏
-
167 收藏
-
224 收藏
-
196 收藏
-
270 收藏
最新阅读
更多>
-
140 收藏
-
147 收藏
-
378 收藏
-
255 收藏
-
287 收藏
-
393 收藏
-
310 收藏
-
110 收藏
-
412 收藏
-
423 收藏
-
274 收藏
-
379 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习