English 中文(简体)
Go s Buffered Channel s Blocking mechanism
原标题:Go s Buffered Channel s Blocking mechanism
  • 时间:2023-12-11 02:56:17
  •  标签:
  • go
  • channel

,在“Go的Tour中,照相代码如下:

package main

import "fmt"

func main() {
    ch := make(chan int, 2)
    ch <- 1
    ch <- 2
    fmt.Println(<-ch)
    fmt.Println(<-ch)
}

罚款和印刷

1
2

这种行为不同于对这一做法的描述,即:

只有在缓冲区满时,才进入缓冲通道。 缓冲区空空时的收货箱

<代码>ch <- 2,ch s full,而且由于我们只管理了一条主要路线的单行路线,因此该行程应封到<编码>ch ,由接收人消费,因此该代码不应达到fmt.Println(<-ch)的线,但应当说像样的东西。

致命的错误:所有活力都是灰色的——僵局!

然而,由于情况并非如此,我非常混淆,并寻求指导。

这里是我写的另外一部法典。

chh := make(chan int, 2)

go func() {
    chh <- 1
    fmt.Printf("chh after 1: %v, %v
", cap(chh), len(chh))
    chh <- 2
    fmt.Printf("chh after 2: %v, %v
", cap(chh), len(chh))
    chh <- 3
    fmt.Printf("chh after 3: %v, %v
", cap(chh), len(chh))
}()

fmt.Println(<-chh)
fmt.Println(<-chh)
fmt.Println(<-chh)

执行的结果是

1
chh after 1: 2, 0
chh after 2: 2, 0
chh after 3: 2, 1
2
3

更令人困惑。 此时还有另一批运送路线。 我期望,在第一个<代码>fmt.Println(<-chh)期间,主要路线应当被阻断。 附表领取人将接手直传匿名功能,并应在<条码>ch <- 2之前执行,然后将自己和附表领取人再次前往主行。 然而,结果显示,第2条路由<条码>chh <- 1立即封。 为什么如此?

问题回答

该频道有两台能力。 两个派团可以成功,不受阻。 页: 1 如果该频道已经满载,则只发送一个区块。 发出。

<>条码> 2. 它可以以最多2个价值观为主,如果你试图把第3个数值推高的话,它就会受阻。 仅举两个价值观。





相关问题
Antlr3 - HIDDEN token in the parser

Can you use a token defined in the lexer in a hidden channel in a single rule of the parser as if it were a normal token? The generated code is Java... thanks

TcpChannel registration problem

I ve read here: Error 10048 when trying to open TcpChannel I am having what I thought to be a similar problem - apparently not. I took the advice of the first respondant to reset winsock (how does ...

get FileChannel without using java.io.* (use pure NIO)

Recently I got a comment to this answer that I should stay away from java.io if I want to use "pure NIO". This is the simplified code (copy a file): private static void copy(File source, File ...

WCF ChannelFactory State Property

What does it mean for a ChannelFactory to have a State property? I understand that a created channel can have connection based states. But am confused as to why the ChannelFactory also has such ...

WCF channel lifetime with repeat calls

Maybe this is an obvious question, maybe it isn t. Imagine a GUI control application where every button push calls a different function on a remote WCF service. Button usage is frequent at ...

WCF duplex channel gets closed when using callbacks

My WCF service uses netTcpBinding, and has a callback object. I need to service multiple concurrent clients, and mantain sessions, so the service is decorated with [ServiceBehavior(...

Shared memory vs. Go channel communication

One of Go s slogans is Do not communicate by sharing memory; instead, share memory by communicating. I am wondering whether Go allows two different Go-compiled binaries running on the same machine to ...