Why Clojure instead of Java for concurrent programming
Asked Answered
K

9

16

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

Kolkhoz answered 15/10, 2009 at 7:30 Comment(0)
T
18

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).

Tyra answered 15/10, 2009 at 10:5 Comment(0)
P
7

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.

Prolocutor answered 15/10, 2009 at 7:32 Comment(2)
@Surya: Apparently they removed the old conference presentations.Prolocutor
@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.Prolocutor
N
6

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.

Niddering answered 15/10, 2009 at 8:29 Comment(1)
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.Nelia
C
5

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.

Childress answered 15/10, 2009 at 7:58 Comment(0)
E
5

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.

Effuse answered 15/10, 2009 at 12:28 Comment(1)
I think you have to wrap the reduce in <code>(seque (reduce (pmap #(stuff) input)))</code>Shows
A
2

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.

Archean answered 17/10, 2009 at 9:23 Comment(0)
Z
2

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. ;-)

Zebulon answered 18/10, 2009 at 5:56 Comment(0)
C
1

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

Component answered 19/10, 2009 at 12:0 Comment(0)
P
-5

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?

Pomcroy answered 15/10, 2009 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.