- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我看了关于 Advanced Go Concurrency Patterns 的精彩视频.一开始,Sameer Ajmani 展示了一个乒乓应用程序。
package main
import (
"fmt"
"time"
)
type Ball struct{ hits int }
func main() {
table := make(chan *Ball)
go player("ping", table)
go player("pong", table)
table <- new(Ball) // game on; toss the ball
time.Sleep(1 * time.Second)
fmt.Println(<-table) // game over; grab the ball
}
func player(name string, table chan *Ball) {
for {
ball := <-table
ball.hits++
fmt.Println(name, ball.hits)
time.Sleep(100 * time.Millisecond)
table <- ball
}
}
代码是如何工作的,我了解 90%。它们是两个 goroutine,它们在主线程休眠期间互相发送消息 ping 和 pong。
然后我尝试跟随
package main
import (
"fmt"
"time"
)
type Ball struct{ hits int }
func main() {
table := make(chan *Ball)
go player("ping", table)
go player("pong", table)
table <- new(Ball) // game on; toss the ball
time.Sleep(1 * time.Second)
fmt.Println(<-table) // game over; grab the ball
fmt.Println(<-table) // game over; grab the ball
}
func player(name string, table chan *Ball) {
for {
ball := <-table
ball.hits++
fmt.Println(name, ball.hits)
time.Sleep(100 * time.Millisecond)
table <- ball
}
}
我在这里陷入了僵局,真的不明白为什么。查看 go 例程中的最后一行,我尝试像倒数第二行一样从 channel 接收值。在后台,两个 goroutines 仍然继续循环并互相发送值。对我来说,它似乎是一个用于表变量 channel 的多接收器。
我的主要问题是,我在第二个样本中遇到了什么僵局?
最佳答案
In background the two goroutines still continue loop and send to each other value.
不,他们没有。
当您使用 make(chan *Ball)
创建 channel 时,你正在制作一个无缓冲的 channel 。这相当于说 make(chan *Ball,0)
,这意味着该 channel 可以容纳 0 个东西 - 或者,更明确地说,对 channel 的任何写入都将阻塞,直到另一个例程从该 channel 读取,反之亦然。
无缓冲 channel 的执行顺序是这样的:
ball := <-table
的表中读取, 直到 table
才被阻止写给ball := <-table
从表中读取, 直到 table
才被阻止写给Main 将 Ball 写入 table
使用以下行:
table <- new(Ball) // game on; toss the ball
这没有被屏蔽,因为有人在 channel 上等待阅读。
ball := <-table
之后继续执行)table <- ball
把球放在 table 上, 未被阻止,因为 pong 正在等待ball := <-table
之后继续执行)table <- ball
把球放在 table 上, 未被阻止,因为 ping 正在等待ball := <-table
之后继续执行)这是使用 channel 确保一次只有一个例程运行的一个很好的例子。
为了结束游戏,主线程在一秒后将球从 channel 中抢走:
time.Sleep(1 * time.Second)
fmt.Println(<-table) // game over; grab the ball
在此之后,table
channel 将是空的,任何进一步的读取都将被阻塞。
player
例程在 ball := <- table
处被阻止.如果你再做一次<-table
在主线程中读取, 读取也会阻塞,直到例程尝试写入表 channel 。但是,由于没有其他例程在运行,因此出现死锁(所有 goroutine 都被阻塞)。
好的。那我可以把两个球放在 channel 里吗?
没有。
如果我们尝试将两个球放入 channel 中,我们可能会得到输出:
Ping 1
Pong 1
因为两者都是player
试图将他们的球放回 channel 中的例程将被卡住 - 但没有人会试图阅读它。订单看起来像这样:
table
(没有因为有人在等待阅读而被阻止)table
(没有因为有人在等待阅读而被阻止)Here's a program to illustrate
正如评论者所指出的,结束游戏的更好方法是关闭 channel 。但是,我希望这个讨论已经消除了您的困惑。
关于multithreading - 尝试两次接收值时出现死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26009501/
我有2个功能: function func1() while true do -- listen on connection end end function func2()
我的问题可能看起来很奇怪,但我想我正面临着 volatile 的问题。对象。 我写了一个这样实现的库(只是一个方案,不是真正的内容): (def var1 (volatile! nil)) (def
由于 maven 支持多线程构建,是否可以同时运行 Sonar 多线程? (例如 mvn sonar:sonar -T 4 ) 我运行了它,当模块报告成功时,它报告整个构建失败并返回 java.uti
我们正在启动一个网站,该网站在短时间内的交易量非常大。它基本上是在给票。该代码是用Java,Spring和Hibernate编写的。我想通过产生多个线程并尝试使用JUnit测试用例来获取票证来模仿高容
我正在尝试访问像素数据并将图像从游戏中的相机保存到磁盘。最初,简单的方法是使用渲染目标,然后使用RenderTarget-> ReadPixels(),但是由于ReadPixels()的 native
我们有以下系统: 用户数:〜500k 项目数:〜100k UserSimilarity userSimilarity = new TanimotoCoefficientSimilarity(dataM
也许这是一个经常出现的问题,但我需要根据我的上下文进行一些自定义。 我正在使用 Spring Batch 3.0.1.RELEASE 我有一个简单的工作,有一些步骤。一个步骤是这样的 block :
也许这是一个经常出现的问题,但我需要根据我的上下文进行一些自定义。 我正在使用 Spring Batch 3.0.1.RELEASE 我有一个简单的工作,有一些步骤。一个步骤是这样的 block :
我正在尝试使用PyBrain和Python的multiprocessing软件包在Python中训练神经网络。 这是我的代码(它训练了一个简单的神经网络来学习XOR逻辑)。 import pybrai
我有一个繁重的功能,不适合在主时间轴上执行(因为要花很长时间才能完成并使程序崩溃)。 因此我在air(as3)中搜索多线程,但是我发现的所有示例都说明了如何在worker中运行单独的swf文件。如何在
我想实现线程A 和线程B 并行运行并共享全局变量。 下面是用python编写的代码。我想在中执行相同操作Dart (我不想使用future等待,因为它正在等待其他线程完成或必须等待。) 大小写变量:
我的一个项目只适用于调试 DLL,而不适用于非调试 DLL。 在 Debug DLL 设置下发布项目有哪些注意事项?例如,是否丢失了某些优化? 如何通过将调试版本设置为非调试 DLL 来调试此项目?我
我正在尝试比较 Matlab 和 Julia 之间的速度和性能。我正在查看一个代码,该代码对承受给定负载的连续体结构进行拓扑优化。我正在查看的代码是公共(public)代码topopt88.m:htt
Serving Flask 应用程序“服务器”(延迟加载) 环境:生产警告:这是一个开发服务器。不要在生产部署中使用它。请改用生产 WSGI 服务器。 Debug模式:开启 在 http://0.0.
我对 PyQT 很陌生。我正在学习如何制作 Progressbar 并随着算法的进展对其进行更新。我已经能够制作一个使用此链接进行 self 更新的基本进度条:Python pyqt pulsing
我正在尝试指定在特定线程上运行任务,这样我就可以使用两个专用于“放入” channel 的耗时任务的线程,而其他线程则用于处理该任务。 我对如何将特定任务分配给特定线程感到困惑。我以为我可以使用类似
我正在编写一个软件,它对很多(潜在的大)图像进行大量图像操作/合成。 多线程有助于提高速度,但 QT 不允许同时在同一图像上使用多个 QPainter。 所以我必须在副本的每个线程中进行图像操作/合成
此脚本读取 url 文件以执行多线程 HTTP 请求。 如何使用带有 url 的数组来发出多线程请求? 我的阵列将有类似的东西: @array = ("https://example.com/xsd"
Java 文档声明了以下关于构造函数同步的内容: Note that constructors cannot be synchronized — using the synchronized keyw
我有一个程序,其中主线程创建了很多线程。它崩溃了,我正在调试核心文件。崩溃发生在其中一个子线程中。为了找到原因,我需要知道主线程是否还活着。有什么方法可以找出哪个线程是初始线程? 最佳答案 Is th
我是一名优秀的程序员,十分优秀!