开发者

Good concurrency example of Java vs. Clojure

Clojure is said to be a language that makes multi-thread programming easier.

From the Clojure.org开发者_开发知识库 website:

Clojure simplifies multi-threaded programming in several ways.

Now I'm looking for a non-trivial problem solved in Java and in Clojure so I can compare/contrast their simplicity. Anyone?


I'd suggest also looking at Christophe Grand's thread safe blocking queue; it's less than 20 lines, but packs a lot of functionality, and in my opinion demonstrates expert use of some of Clojure's concurrency features, immutability, atoms, and lazy sequences.

Consider that the Java alternative java.util.concurrent.LinkedBlockingQueue is 842 lines of subtle (arguably complex) commented code, and you begin to understand how Clojure really does deliver on its concurrency promise; significantly raising the level of abstraction and delivering a correct implementation with approximately 10-20x less code.

You'll also notice that the when reading the Java code it is really hard to see the forest for the trees... If you were given it could you be sure of its correctness by looking at it? Also bear in mind that this code was written by Doug Lea (arguably the foremost expert on Java Concurrency) and is for java highly readable; I very much doubt I could write readable performant code such as this in Java quickly and be sure of its correctness.

Contrast this to the Clojure version and once familiar with the basics of Clojure it's easy to tease apart and understand how it works... Within 20 minutes I was able to understand every line of the implementation, and be sure of its correctness. And now that I'm far more familiar with Clojure's idioms and FP, I'd guess this would now take me closer to 5 minutes. I'd also probably be able to write "correct" code like this in Clojure in hours or minutes.

Christophes clojure wrapper of the above java class is also instructive as it shares the same functional interface as the first version.


The Wide Finder Project, started by Tim Bray, has a number of Clojure entries -- the most notable of which is by Alex Osborne; he's done a fantastic write-up on it -- as well as entries in Java, Scala and an impressive number of other languages.

The problem being solved is thoroughly practical and rather interesting as a parallelisation challenge and Tim's postings about it (see also the more recent Wide Finder 2 series) are quite enjoyable to read (and carry a good educational value). Plus Alex's text is really great, please read it even if you decide to skip the rest. It uses some Clojure-specific features (like Atoms) alongside stuff brought over from Java were it does the job fine & fast... In fact, it would be interesting just for the way in which it showcases Clojure's excellent Java interop.

All in all, I'd say this is likely the best thing to start with.


Please look to Rich Hickey's Ants example - it shows how to use clojure's features to build concurrent programs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜