14

How can scala make writing multi-threaded programs easier than in java? What can scala do (that java can't) to facilitate taking advantage of multiple processors?

2
  • 4
    Java can do anything Scala can do, and vice versa. They both run in the JVM, and they're both Turing-complete, so by definition either one can do the same things as the other. The difference is what's easier or more natural in one language than the other. Commented Aug 20, 2010 at 23:10
  • @cHao @Zan edited question to more closely address the issue. :) Commented Aug 20, 2010 at 23:15

3 Answers 3

15

The rules for concurrency are

1 avoid it if you can

2 share nothing if you can

3 share immutable objects if you can

4 be very careful (and lucky)

For rule 2 Scala helps by providing a nicely integrated message passing library out of the box in the form of the actors.

For rule 3 Scala helps to make everything immutable by default.

For rule 4 Scala's flexible syntax allows the creation of internal DSL's making it easier and less wordy to express what you need consicely. i.e. less place for surprises (if done well)

Sign up to request clarification or add additional context in comments.

3 Comments

I think #1 is increasingly becoming difficult to do. I'd say that using a framework, e.g. map-reduce or some fork-join library, can help you write concurrent programs without having it feel concurrent.
I agree. It definitely pays to carefully examine your needs and select concurrency patterns which fit these. And then use a debugged library/framework which implement these.
#1 Needs to come of the list; rather we should be actively looking for ways to exploit the multiple CPU cores available on modern computers.
12

If one takes Akka as a foundation for concurrent (and distributed) computing, one might argue the only difference is the usual stuff that distinguishes Scala from Java, since Akka has both Java and Scala bindings for all its facilities.

Comments

6

There is nothing Scala does that Java does not. That would be silly. Scala runs on the same JVM that Java does.

What Scala does do is make it easier to write, easier to reason about and easier to debug a multi-thread program.

The good bits of Scala for concurrency are its focus on immutable objects, its message-passing and its Actors.

This gives you thread-safe read-only data, easy ways to pass that data to other threads, and easy use of a thread pool.

2 Comments

I'd add closures to it. It really makes a lot of things easier.
I'd also add the delimited continuations really change the playing field for writing concurrent programs. Hopefully the amazing library support pops up in a few months.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.