本文介绍了去频道vs Java BlockingQueue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Go频道和Java BlockingQueue之间有什么区别吗?两者都是具有相似阻塞和内存模型语义的队列。我可以说最大的不同是Go频道支持 select 语句,它允许您执行一个通道操作。一个示例(从 Go语言说明中更改): select { case i1 =< -c1: print(received,i1,from c1 \\\) case c2 print(sent,i2,to c2 \\\) case i3,ok:=(< -c3):// same as :i3,ok:=< -c3 if ok { print(received,i3,from c3 \\\)} else { print( c3 is closed'\)} } 在此例如,将执行receive-from-c1,send-to-c2或receive-from-c3操作中的一个。当输入选择时,随机选择一个就绪通道(如果有的话)。否则,操作会阻塞,直到其中一个通道准备就绪。 我不知道使用Java实用程序对此通道选择进行建模的任何方法。有人可能会争辩说,这是 select 语句的属性,而不是通道的设计,但我认为它是设计通道的基础。 Are there any differences between a Go channel and a Java BlockingQueue? Both are queues with similar blocking and memory model semantics. Optionally both can have a capacity set. 解决方案 I would say the biggest difference is that Go channels have support for the select statement, which allow you to perform exactly one channel operation. An example (altered from the Go language specification):select {case i1 = <-c1: print("received ", i1, " from c1\n")case c2 <- i2: print("sent ", i2, " to c2\n")case i3, ok := (<-c3): // same as: i3, ok := <-c3 if ok { print("received ", i3, " from c3\n") } else { print("c3 is closed\n") }}In this example, exactly one of the receive-from-c1, send-to-c2, or receive-from-c3 operations will be performed. When entering the select, a ready channel (if any) is selected randomly. Otherwise, the operation blocks until one of the channels is ready.I'm not aware of any trivial way to model this channel selection using the Java utilities. One could argue that this is a property of the select statement rather than the design of channels, but I would argue that it's fundamental to the design of channels. 这篇关于去频道vs Java BlockingQueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-22 23:30