Maison > développement back-end > Golang > le corps du texte

Goroutine s'endort prématurément

PHPz
Libérer: 2024-02-09 11:30:31
avant
1118 Les gens l'ont consulté

Goroutine 过早进入睡眠状态

php小编香蕉在这里为大家介绍一种常见的问题,即Goroutine过早进入睡眠状态。在Go语言中使用Goroutine并发执行任务是非常高效的,但有时候我们会遇到这样的情况,即Goroutine在执行任务之前就进入了睡眠状态,导致任务无法正常进行。本文将详细解释这个问题的原因,并提供解决方法,帮助大家更好地理解和使用Goroutine。

问题内容

代码:

package main

import (
    "fmt"
    "time"
)

func main() {
    link := make(chan bool)
    stop := make(chan bool)
    go a(link, stop)
    go b(link)
    <-stop
}

func a(link chan bool, stop chan bool) {
    for i := 0; i < 20; i++ {
        time.Sleep(1 * time.Second)
        link <- true
    }
    stop <- true
}

func b(link chan bool) {
    go func() {
        <-link
        fmt.Println("A")
    }()
    go func() {
        <-link
        fmt.Println("B")
    }()
}
Copier après la connexion

这段代码没有做任何特别的事情,我只是想理解通道和 goroutine。但出了点问题,协程在两次循环和应用程序崩溃后进入休眠状态。

让我们分析一下它的作用 - 每隔一秒消息发送到 link。函数 b 在两个地方接收它,因此调用了 print a 和 b。所以基本上每一秒 a 和 b 都应该出现在控制台中。但由于我不知道的原因,它没有发生,程序打印 a 和 b 一次,然后崩溃。 我可能对这个概念理解不够(golang确实不直观),所以我希望我能在这里得到答案。

解决方法

只有当有 goroutine 从中接收数据时,发送到无缓冲通道的通道才会成功。对于前两次发送,有 goroutine 从 link 通道接收。但是它们收到一个值并终止,并且不再有任何 goroutine 从 link 接收,因此第三个发送到 link 的块。由于没有其他 goroutine 运行,程序出现死锁。

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:stackoverflow.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!