aspose file tools*
The moose likes Scala and the fly likes What can Scala do for concurrent programming that Java can't? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "What can Scala do for concurrent programming that Java can Watch "What can Scala do for concurrent programming that Java can New topic
Author

What can Scala do for concurrent programming that Java can't?

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hello again :-)

What puzzles me is the hype about Scala and in particular its pathbreaking concurrency features. Is there something new to it that can't technically be achieved with Java? Or is it more the way you have to think about concurrency problems in functional programming languages? Obviously there are actor implementations for Java, too, so I guess it's not impossible to achieve the same things in Java. But does anyone know what's the reason that this other way of thinking about concurrency hasn't gained broad popularity before?

Marco
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Marco,
I'm not a Scala programmer, but I've been listening to some who are and as I understand it the main thing is that the tools for concurrency in Java are very low level. There's a great article on this at IBM Developer Works, here's the link to it: http://www.ibm.com/developerworks/java/library/j-scala02049.html

Burk


SCJP, SCJD, SCEA 5 "Any sufficiently analyzed magic is indistinguishable from science!" Agatha Heterodyne (Girl Genius)
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Burk,

thanks for the interesting link. I'm going to read up on this ;-)

All these discussions about "low-level concurrency" in Java is something I don't really understand. Of course Java has all the things like the wait() or notify() methods or intrinsic locks etc. But do you really have to use it? At least starting with Java 5 there are a lot of higher level utilities, too. In my opinion most people who complain about the low-level support just use the wrong tools. What's easier than using a ThreadPoolExecutor for example which takes care of managing a thread pool for you? Of course you always have to be careful with shared state and all of its implications for concurrency. But that doesn't mean that it wouldn't be possible to create immutable objects and methods without side effects. Most programmers just don't care about concurrency issues because for a lot of traditional applications concurrency errors at runtime may be rare.

That said I really like the concepts and possibilites of Scala and I'm definitely going to put more effort into learning Scala. I just wanted to point out that a lot of the "advantages" could be achieved in Java, too. Maybe some things are harder, but not impossible. On the other hand I'm sure there are good reasons for all the hype about Scala's concurrency features, which I'd like t hear!

Marco
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Marco,
I agree with you. The thing is that while you can make classes thread-safe in Java (if you know what you're doing ), Scala makes it easy and natural to do - or at least that's my understanding of it. I think the reason we're seeing more about concurrency is that it's more common to see machines with multiple processors, or multiple cores, now.
Burk
Gabriel Claramunt
Ranch Hand

Joined: May 26, 2007
Posts: 375

One of the advantages of Scala is the power of having all the high level concurrency constructs in libraries but with the "feel" of being part of the core language.


Gabriel
Software Surgeon
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

The thing is that while you can make classes thread-safe in Java (if you know what you're doing ), Scala makes it easy and natural to do - or at least that's my understanding of it.

That's really true! If you're concerned about code quality and don't want to ignore the fact that code for multi-threaded environments simply has to be thread-safe to work correct in all situations, you soon start to realize how much trouble even simple data types like "Date" can cause because they are not immutable ;-)

That said, I understand that Scala encourages good coding habits and makes it easier to write thread-safe code. Anyway, I'd be glad to hear some not so well-known advantages Scala gives you for concurrent applications - additionally to built-in concepts for immutable value objects or side-effect free functions!

Marco
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
That said, I understand that Scala encourages good coding habits and makes it easier to write thread-safe code. Anyway, I'd be glad to hear some not so well-known advantages Scala gives you for concurrent applications - additionally to built-in concepts for immutable value objects or side-effect free functions!


We should probably ask the experts for some of that, but the other thing I've heard is that Scala it considered "low ceremony" compared to Java in that it doesn't need a lot of the boilerplate code that goes along with checked exceptions.
Venkat Subramaniam
Author
Ranch Hand

Joined: Jan 28, 2008
Posts: 79
This is a very reasonable question and I think the discussion that followed is pretty good as well. To add my 2 cents:
It is possible to write OO code in 'C' but would you? In a similar way, it is possible to write thread safe code in Java, but the
efforts are way too much. Scala makes it much easier. We still need discipline and good coding practices. However, it is easier
(a lot more easier) to follow a small set of these practices and write thread safe code in Scala than in Java. You can add more
libraries to Java, however, the idiomatic differences (immutability, side-effect free functions, closures, working with immutable
datastructures) along with powerful libraries of Scala and features like traits change the way you program and hence greatly
eases the burden.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Venkat,

thanks for clarifaction! So it seems we were on the right track

Of course you're right, just because something CAN be done in one programming language it's not necessarily the best or easiest way. Anyway, I'm astonished that better support for a few basic concepts like immutability for example obviously make a big difference.

Marco
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Venkat,
As time goes on, I've noticed that making something easy to do is often the little push needed to get more folks to do what they know they should be doing anyway.
Thanks for the reminder,
Burk
 
jQuery in Action, 2nd edition
 
subject: What can Scala do for concurrent programming that Java can't?