views:

1048

answers:

9

When Java is providing the capabilities for concurrent programming, what are the major advantages in using Clojure (instead of Java)?

+7  A: 

Without being an expert on Clojure I would say that the main advantage is that Clojure hides a lot of the details of concurrent programming and as we all know the devil is in the details, so I consider that a good thing.

You may want to check this excellent presentation from Rick Hickey (creator of Clojure) on concurrency in Clojure. EDIT: Apparently JAOO has removed the old presentations. I haven't been able to locate a new source for this yet.

Brian Rasmussen
the links seems to broken. Anyone have a working one ?
Surya
@Surya: Apparently they removed the old conference presentations.
Brian Rasmussen
Were you thinking of this presentation?http://clojure.blip.tv/file/812787/
Gabor Kulcsar
@Gabor. Thanks for the link. I am not sure it is the same presentation. Its a little hard to tell from the video, but it looks good nonetheless.
Brian Rasmussen
A: 

Java programming language evolution is quite slow mainly because of Sun's concern about backward compatibility.

Why don't you want just directly use JVM languages like Clojure and Scala?

denis.zhdanov
+4  A: 

Because the world has advanced in the past 10 years and the Java language (!= the JVM) is finding it hard to keep up. More modern languages for the JVM are based on new ideas and improved concepts which makes many tedious tasks much more simple and safe.

Aaron Digulla
+6  A: 

Because Clojure is based on the functional-programming paradigm, which is to say that it achieves safety in concurrency by following a few simple rules:

  • immutable state
  • functions have no side effects

Programs written thus pretty much have horizontal scalability built-in, whereas a lock-based concurrency mechanism (as with Java) is prone to bugs involving race conditions, deadlocks etc.

oxbow_lakes
Lisps aren't purely functional, it's perfectly possible to have lots of mutable data structures and side effects and mess those up when going concurrent. But yeah, a functional programming advocate will avoid side effects like the plauge, making concurrency much easier.
delnan
+17  A: 

Clojure is designed for concurrency.

Clojure provides concurrency primitives at a higher level of abstraction than Java. Some of these are:

  • A Software Transactional Memory system for dealing with synchronous and coordinated changes to shared references. You can change several references as an atomic operation and you don't have to worry about what the other threads in your program are doing. Within your transaction you will always have a consistent view of the world.

  • An agent system for asynchronous change. This resembles message passing in Erlang.

  • Thread local changes to variables. These variables have a root binding which are shared by every thread in your program. However, when you re-bind a variable it will only be visible in that thread.

All these concurrency primitives are built on top of Clojures immutable data structures (i.e., lists, maps, vectors etc.). When you enter the world of mutable Java objects all of the primitives break down and you are back to locks and condition variables (which also can be used in clojure, when necessary).

Jonas
+5  A: 

One of the cool things about having immutable types is that most of the built-in functions are already multi-threaded. A simple 'reduce' will span multiple cores/processors, without any extra work from you.

So, sure you can be multi-threaded with Java, but it involves locks and whatnot. Clojure is multi-threaded without any extra effort.

nilamo
I think you have to wrap the reduce in <code>(seque (reduce (pmap #(stuff) input)))</code>
Arthur Ulfeldt
+2  A: 

Yes, Java provides all necessary capabilities for concurrent programs.

An analogy: C provides all necessary capabilities for memory-safe programs, even with lots of string handling. But in C memory safety is the programmer's problem.

As it happens, analyzing concurrency is quite hard. It's better to use inherently safe mechanisms rather than trying to anticipate all possible concurrency hazards.

If you attempt to make a shared-memory mutable-data-structure concurrent program safe by adding interlocks you are walking on a tightrope. Plus, it's largely untestable.

One good compromise might be to write concurrent Java code using Clojure's functional style.

DigitalRoss
+2  A: 

In addition to Clojure's approach to concurrency via immutable data, vars, refs (and software transactional memory), atoms and agents... it's a Lisp, which is worth learning. You get Lisp macros, destructuring, first class functions and closures, the REPL, and dynamic typing - plus literals for lists, vectors, maps, and sets - all on top of interoperability with Java libraries (and there's a CLR version being developed too.)

It's not exactly the same as Scheme or Common Lisp, but learning it will help you if you ever want to work through the Structure and Interpretation of Computer Programs or grok what Paul Graham's talking about in his essays, and you can relate to this comic from XKCD. ;-)

Anon
+1  A: 

This video presentation makes a very strong case, centred around efficient persistent data structures implemented as tries.

Timothy Pratley