Channels in Groovy

import java.util.concurrent.BlockingQueue
import java.util.concurrent.LinkedBlockingQueue

def main() {
    // Create a new BlockingQueue. In Groovy, we use BlockingQueue
    // as an equivalent to Go's channels.
    BlockingQueue<String> messages = new LinkedBlockingQueue<>()

    // Send a value into the queue using the `put` method.
    // Here we send "ping" to the messages queue we made above,
    // from a new thread.
    Thread.start {
        messages.put("ping")
    }

    // The `take` method receives a value from the queue.
    // Here we'll receive the "ping" message we sent above and print it out.
    String msg = messages.take()
    println(msg)
}

main()

In Groovy, we don’t have channels as in Go, but we can use BlockingQueue as a similar concept. BlockingQueue allows threads to communicate by sending and receiving elements.

When we run the program, the “ping” message is successfully passed from one thread to another via our queue.

$ groovy channels.groovy
ping

By default, put and take operations block until the operation can be performed. This property allowed us to wait at the end of our program for the “ping” message without having to use any other synchronization.

In Groovy, we use threads instead of goroutines. The Thread.start block is equivalent to Go’s go keyword for starting a new concurrent operation.

The BlockingQueue interface in Java (which Groovy uses) provides thread-safe operations for adding and removing elements, making it suitable for producer-consumer scenarios similar to Go’s channels.

Remember that while this example demonstrates a similar concept to Go’s channels, Groovy and Java’s concurrency models have their own unique features and best practices that should be considered for more complex scenarios.