Go channel vs Java BlockingQueue

后端 未结 4 682
日久生厌
日久生厌 2021-02-01 23:55

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 se

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-02 00:14

    One more very important difference is: You can close a Go channel to signal that no more elements are coming. That is not possible using Java.

    Example: goroutine A reads a list of files. It posts each file into the Channel. After the last file, it closes the channel. goroutine B reads the files from the channel and processes them in some way. After the channel is closed, the goroutine quits.

    Doing this in Java is not easily possible; however some workarounds exist.

提交回复
热议问题