doukunsan5553 2012-05-21 18:14
浏览 22
已采纳

转到频道与Java BlockingQueue

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.

  • 写回答

4条回答 默认 最新

  • doucuyu2259 2012-05-21 22:12
    关注

    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
    ")
    case c2 <- i2:
        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
    ")
        }
    }
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号