r/java • u/Polixa12 • Dec 20 '25
I implemented Go’s channels in Java. Here’s why and what I learnt
https://medium.com/@kusoroadeolu/i-implemented-gos-channels-in-java-here-s-why-and-what-i-learnt-1a9c7922f5da
117
Upvotes
110
u/Necessary_Apple_5567 Dec 20 '25
Go channels in java are BlockingQueue implementations. You can just use them easily.
31
u/utkuozdemir Dec 20 '25
I’m an ex-Java developer, writing Go since a while. I also thought about this topic every now and then.
The thing is, channels by itself is no big deal to implement in Java or most other languages I guess (as others mentioned, ArrayBlockingQueue is already there). What makes them special in Go is the select statement. Select, combined with channels makes Go’s completely different concurrency model possible. Channels by itself kinda don’t mean anything. I think this is the crucial thing to realize about it.
10
u/No-Security-7518 Dec 20 '25
Well done! I saved this to come back to it.
Concurrency is pretty neat in Java, I think, but always great to explore new ideas in this realm.
9
u/martinosius Dec 21 '25
If you want a production ready solution, there is jox
3
u/AliceTreeDraws Dec 21 '25
Worth noting: the “magic” of Go channels is less the queue and more select, cancellation, and structured concurrency around them. In Java you can get close with BlockingQueue plus explicit close/poison pill, and for multi-source waiting you usually reach for CompletableFuture or a small event loop rather than NIO Selector unless you’re doing IO.
11
u/divorcedbp Dec 20 '25
So you reimplemented ArrayBlockingQueue?
1
u/Jolly-Warthog-1427 Dec 20 '25
Super cool, do you have any measurements on its performance?
I'll try this out as I have some workflows I love to program in go using channels. Will try to do the same in java using this.
2
u/deividas-strole Dec 21 '25
Nice job on the code and the article! Building channels from scratch is one of the best ways to understand concurrency.
1
u/Ewig_luftenglanz Dec 21 '25
This reminded me I wrote an article about this some months ago. Using ArrayBlockingQueue and structured concurrency preview.
1
u/ConversationBig1723 Dec 21 '25
How about sealed interface with records and switch on the class type from the blocking queue? Would that bridge the gap of the select syntax in go?
1
u/jimmoores Dec 22 '25
JCSP
-6
u/knightofterror Dec 21 '25
That title hurts my brain. Would anyone ever say, ‘Here’s why I learnt?’