Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Goroutines in Clojure / Java

I recently enjoyed watching the Google IO talk on Go Concurrency patterns

Although the Go approach to concurrency (groutines, communication over channels) is clearly different to Clojure (immutability, manged references, STM), it seemed like the Go approach could still be useful in some circumstances in a Clojure context.

So is there a direct equivalent in Clojure or Java for Go's concurrency primitives (perhaps a library), in particular:

  • channel-like objects that block until a reader and writer are available at both ends
  • A select-like construct that can wait for results on multiple channels

P.S. Perfectly happy with a Java solution since it would be easy to use from Clojure

UPDATE Since the question was originally asked, Clojure now has core.async which provides all this functionality and more.

like image 441
mikera Avatar asked Jul 02 '12 18:07

mikera


People also ask

Are Goroutines just threads?

Goroutine is a lightweight thread in Golang. All programs executed by Golang run on the Goroutine. That is, the main function is also executed on the Goroutine. In other words, every program in Golang must have a least one Goroutine.

How are Goroutines different from Java threads?

Threads are hardware dependent. Goroutines have easy communication medium known as channel. Thread does not have easy communication medium. Due to the presence of channel one goroutine can communicate with other goroutine with low latency.

Are Goroutines processes or threads?

A goroutine is a lightweight thread managed by the Go runtime.

What are Goroutines?

A goroutine is a lightweight execution thread in the Go programming language and a function that executes concurrently with the rest of the program. Goroutines are incredibly cheap when compared to traditional threads as the overhead of creating a goroutine is very low.


1 Answers

You should check out core.async. It is a library written by Rich Hickey that implements go style channels.

like image 146
sbochins Avatar answered Oct 12 '22 03:10

sbochins