通过对通道进行多次写入来理解 golang 阻塞通道行为
php小编柚子在这篇文章中将向大家介绍如何通过对通道进行多次写入来理解 golang 阻塞通道的行为。在golang中,通道是一种用于在协程之间传递数据的重要机制。当通道已满时,写入操作会被阻塞,直到通道有空闲位置。我们将通过一个简单的示例来演示这种行为,并解释阻塞通道的原理和使用方法。无论是初学者还是有经验的golang开发者,都能从本文中获得有益的知识和实践经验。让我们开始吧!
问题内容
我是 golang 新手,正在尝试了解该语言中的并发性。我有一个代码,可以将一些值推送到通道,然后读取它们。
package main import ( "log" "time" ) func Greet2(c chan string) { // logging to Stdout is not an atomic operation // so artificially, sleep for some time time.Sleep(2 * time.Second) // 5. until below line reads and unblock the channel log.Printf("5. Read Greet2:: %s\n\n", <-c) } func Greet(c chan string) { // 4. Push a new value to the channel, this will block // Process will look for other go routines to execute log.Printf("4. Add 'Greet::John' to the channel, block until it is read. Remember, 'Greet' goroutine will block and only other goroutines can run even though this go routine can pull the value out from the channel.\n\n") c <- "Greet::John!" // 8. This statement will never execute log.Printf("8. Read Greet:: %s !\n\n", <-c) } func main() { c := make(chan string) log.Println("1. Main start") // 2. Both go routine will be declared and both will // for a value to be inserted in the channel log.Println("2. Declare go routines.\n\n") go Greet(c) go Greet2(c) // 3. write will block log.Println("3. Add 'main::Hello' to the channel, block until it is read. Remember, 'main' goroutine will block and only other goroutines can run even though this go routine can pull the value out from the channel.\n\n") c <- "main::Hello" // Sleep to give time goroutines to execute time.Sleep(time.Second) // 6. read the channel value. log.Printf("6. Read main:: %s \n\n", <-c) // 7. Insert a new value to the channel log.Println("7. Add 'main::Bye' to the channel, block until it is read.\n") c <- "main::Bye" // Sleep to give time goroutines to execute time.Sleep(time.Second) log.Println("9. Main stop") }
上述程序的输出是
2023/09/02 21:58:07 1. Main start 2023/09/02 21:58:07 2. Declare go routines. 2023/09/02 21:58:07 3. Add 'main::Hello' to the channel, block until it is read. Remember, 'main' goroutine will block and only other goroutines can run even though this go routine can pull the value out from the channel. 2023/09/02 21:58:07 4. Add 'Greet::John' to the channel, block until it is read. Remember, 'Greet' goroutine will block and only other goroutines can run even though this go routine can pull the value out from the channel. 2023/09/02 21:58:10 5. Read Greet2:: main::Hello 2023/09/02 21:58:11 6. Read main:: Greet::John! 2023/09/02 21:58:11 7. Add 'main::Bye' to the channel, block until it is read. 2023/09/02 21:58:11 8. Read Greet:: main::Bye ! 2023/09/02 21:58:12 9. Main stop
我无法理解为什么 4.
(另一个写入通道)在 5.
(第一次从通道读取)之前执行,因为 3.
将阻塞,并且在读取值之前通道不可用来自它(在步骤 5.
中)。我是否误解了阻塞行为,在步骤 3.
中,只有 main
goroutine 块和 Greet
(在步骤 4.
中)可以向通道写入附加值?一个解释确实可以解决我的困惑:)
干杯, DD。
<小时/>感谢您的回复,我已经创建了一个更简单的程序来演示。并发
package main import ( "fmt" ) func do2(c chan int) { fmt.Println(<-c) } func do(c chan int) { // 4. this statement is trying to write another value "2" to the channel // Channel already contains "1" as the value which has not been read yet. // this statement will wait for "1" to get read and block the execution. // Scheduler will look for other goroutines that can execute. // However, this("do") is blocked as well as "main" is blocked too and // there are no other goroutines to execute. // Hence, will result in a "Deadlock" fatal error. c <- 2 fmt.Println(<-c) } func main() { // 1. Declare a channel c := make(chan int) // 2. Declare "do" goroutine go do(c) // 3. write "1" to the channel // This will block and wait for program's other goroutines to read the value. // however, there is only "do" goroutine is defined can run at this point. // Scheduler, will try to run "do" goroutine. c <- 1 go do2(c) }
死锁
可以通过交换c <- 1
和go do2(c)
语句来修复。死锁
可以通过交换c <- 1
和go do2(c)
语句来修复。
解决方法
在 Go 中,当您在通道上发送值时(步骤 3 中的 c <- "main::Hello"
),发送 Goroutine 将阻塞,直到有另一个 Goroutine 准备好从通道接收值。然而,这并不意味着没有其他 goroutine 可以继续执行。在您的代码中, Greet
和 Greet2
c <- "main::Hello"
),发送 Goroutine 将阻塞,直到有另一个 Goroutine 准备好从通道接收值。然而,这并不意味着没有其他 goroutine 可以继续执行。在您的代码中, - 协程都在等待来自通道的值,因此当您在步骤 3 中发送值时,其中一个(不保证是哪一个)将解除阻塞并继续执行。
-
c
让我一步步分解事件的顺序: -
Greet
和Greet2
主程序启动,您创建一个频道 。 -
Greet
或Greet2
您声明了两个 goroutine, ,并且两者都在等待来自通道的值。 -
Greet
解除阻塞并继续执行。它记录消息“4. 将‘Greet::John’添加到频道...”并发送“Greet::John!”在频道上。这会再次阻塞Greet
您在通道上发送一个值“main::Hello”,这会阻塞主 goroutine,直到其他 goroutine 从通道读取数据。但是,两个 Goroutine 之一( )不会被阻止接收该值。 -
Greet2
,因为此时没有其他 goroutine 可以从通道中读取。 - 解除阻塞并继续执行。它记录消息“5. Read Greet2:: main::Hello”并从通道读取值“main::Hello”。
Greet
在向通道写入时仍被阻止,而Greet2
Main 解锁,记录“6. Read main:: Greet::John!”并写着“问候::约翰!”来自频道。 -
Greet
Main 在通道上发送另一个值“main::Bye”。此时, 在向通道写入时仍被阻止,而 - 因未从通道读取而被阻止。
仍然在写入时被阻止,因此它永远不会记录“8. Read Greet:: main::Bye !”Greet2
碰巧首先被解锁,但也可能是 Greet
🎜因此,理解此处行为的关键是,当您在通道上发送值时,它会解锁任何正在等待从通道读取数据的 goroutine。等待的 goroutine 解除阻塞的顺序是不确定的,取决于调度程序。在您的情况下,🎜 碰巧首先被解锁,但也可能是 🎜。🎜 🎜总之,您观察到的行为与 Go 通道的工作方式完全一致,并且需要注意的是,竞争 Goroutines 之间的执行顺序无法保证。🎜
以上是通过对通道进行多次写入来理解 golang 阻塞通道行为的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

利用Golang开发功能强大的桌面应用随着互联网的不断发展,人们已经离不开各种类型的桌面应用程序。而对于开发人员来说,如何利用高效的编程语言来开发功能强大的桌面应用至关重要。本文将介绍如何利用Golang(Go语言)来开发功能强大的桌面应用,并提供一些具体的代码示例。Golang是一种由Google开发的开源编程语言,它具有简洁、高效、并发性强等特点,非常适

Golang开发中的安全性挑战:如何避免被利用用于病毒制作?随着Golang在编程领域的广泛应用,越来越多的开发者选择使用Golang来开发各种类型的应用程序。然而,与其他编程语言一样,Golang开发中也存在着安全性挑战。特别是,Golang的强大功能和灵活性也使其成为潜在的病毒制作工具。本文将深入探讨Golang开发中的安全性问题,并提供一些方法来避免G

标题:在Mac上使用Golang编程的步骤与技巧在当前软件开发领域,Golang(也被称为Go)作为一种高效、简洁、并发性强的编程语言,受到越来越多开发者的关注和使用。在Mac平台上进行Golang编程,可以借助一些工具和技巧,提高开发效率。本文将介绍在Mac上使用Golang编程的步骤与技巧,并提供具体代码示例,帮助读者更好地理解和应用。步骤1:安装Gol

Golang(简称Go语言)作为一种编程语言在近年来逐渐在区块链领域崭露头角,其高效的并发处理能力和简洁的语法特点使其成为了区块链开发中备受青睐的一种选择。本文将探讨Golang如何助力区块链发展,并通过具体的代码示例展示其在区块链应用中的优越性。一、Golang在区块链领域的优势高效的并发处理能力:区块链系统中的节点需要同时处理大量的事务和数据,而Gola

Golang是一种由Google开发的开源编程语言,被广泛应用于后端服务开发、云计算、网络编程等领域。作为一种静态类型语言,Golang具有高效的并发模型和强大的标准库,因此备受开发者青睐。然而,在实际开发中,Golang开发者通常需要结合其他编程语言来进行项目开发,以满足不同场景的需求。PythonPython是一种面向对象的编程语言,以其简洁明了、易于学

Gokit是一个Golang微服务框架,通过优化、可扩展、可维护和测试友好功能,提升API性能。它提供一系列工具和模式,使用户能够快速构建高性能和可维护的API。实际生产中,它被广泛应用于Netflix、Spotify和Uber等大型平台的API构建,处理着海量的请求。

Golang语言作为一门快速、高效的编程语言,受到越来越多开发者的青睐。然而,在日常开发中,我们难免会遇到各种各样的错误。如何正确分析错误的原因,并找到解决策略,是每个Golang开发者需要深入了解和掌握的重要技能。错误分析类型断言失败Golang中,类型断言是常见的操作,但有时如果类型不匹配,就会导致panic。下面是一个简单的例子:varxinter

标题:Golang替换空格的实用技术分享随着互联网的普及和发展,程序设计和开发变得愈发重要。在编程过程中,数据处理是一个非常关键的部分。处理文本数据时,常常会遇到需要替换空格的情况,本文将分享如何在Golang中实现替换空格的技术,并提供具体的代码示例。为什么需要替换空格?空格是文本数据中常见的特殊字符之一,有时候我们需要将空格替换成其他字符,或者去除掉空格
