The Artima Developer Community
Sponsored Link

Artima Developer Spotlight Forum
Exploring Scala's Support for Concurrent Programming

2 replies on 1 page. Most recent reply: Mar 10, 2009 1:04 PM by Raoul Duke

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Exploring Scala's Support for Concurrent Programming Posted: Feb 10, 2009 5:39 PM
Reply to this message Reply

Efficient APIs for concurrent programming are a welcome addition to any developer's toolkit in the age of multi-core CPUs. Java 5's concurrency utilities made Java parallel programming easier by relieving developers of much of the tedium involved in having to deal with low-level concurrency details.

In his latest IBM developerWorks Scala series, Explore Scala concurrency, Ted Neward describes how the Scala language and its APIs provide even more concise tools for parallel programming. Neward's article doesn't discuss Actors, but focuses instead on classes in the concurrent Scala API package:

Like Scala's sister language in the .NET space, F#, Scala stands as one of several purported solutions to the "concurrency problem." Scala's support for concurrency builds, at some level, on top of the features and functionality provided by the JVM and supporting libraries...

Scala provides two levels of support for concurrency, much as it does for other Java-related topics:

  • Full access to the underlying libraries (such as java.util.concurrent) and support for the "traditional" Java concurrency semantics (such as monitors and wait()/notifyAll()).
  • The second, a layer of abstraction on top of those basic mechanics, as exemplified by the MailBox class ... and the Actors library...

The goal is the same in both cases: to make it easier for developers to focus on the meat of the problem rather than having to think about the low-level details of concurrent programming...

In the article, Neward converts a producer/consumer example from Java into Scala code, applying an increasing depth of Scala idioms:

One way to start working with concurrency in Scala is to simply translate the Java code directly over to Scala, taking advantage of Scala's syntax in places to simplify the code...

In Scala, "synchronized" isn't a keyword, it's a method defined on the class AnyRef, the Scala "root of all reference types." This means that to synchronize on a particular object, you simply call the synchronize method on that object...

In the latter half of his article, Neward describes the MailBox class:

MailBox is essentially ... a single-slot buffer that holds a piece of data until it has been retrieved... The big advantage of MailBox is that it completely encapsulates the details of the sending and receiving behind a combination of pattern-matching and case classes, making it more flexible...

MailBox has two basic operations: send and receive. The receiveWithin method is simply a timeout-based version of receive. MailBox takes messages that can be of any type whatsoever. The send() method essentially drops the message into the mailbox, notifying any pending receivers immediately if it's of a type they care about, and appending it to a linked list of messages for later retrieval. The receive() method blocks until a message appropriate to the function block that's passed in to it is received...

No explicit locking required, and no thinking about monitors.

What do you think of Scala's concurrency support?

Vijay Kandy

Posts: 37
Nickname: vijaykandy
Registered: Jan, 2007

Re: Exploring Scala's Support for Concurrent Programming Posted: Feb 11, 2009 9:21 AM
Reply to this message Reply
The mailbox concept coupled with pattern matching ability to process messages looks like a very clean Erlang-like message passing model. However, I think calling receive() on MailBox for both putting and taking a message is a bit confusing. I don’t know, a different method (like send for sending) would have been nicer.

Raoul Duke

Posts: 127
Nickname: raoulduke
Registered: Apr, 2006

Re: Exploring Scala's Support for Concurrent Programming Posted: Mar 10, 2009 1:04 PM
Reply to this message Reply
> What do you think of Scala's concurrency support?

for crying out loud, the message passing is only a small sliver of the Erlang solution. the underlying isolation and super light-weightness of processes is key. the hot-code swap is key. the distribution ability is key. OTP is key.

Flat View: This topic has 2 replies on 1 page
Topic: The Adventures of a Pythonista in Schemeland: Functional Streams Previous Topic   Next Topic Topic: Scaling JavaFX Applications Across Devices

Sponsored Links


Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use