Being quite a fan of Groovy (which also is related to Spring in some way), I nevertheless experimented with Scala a bit.

Trying not to follow the current hype about this language, I mainly wanted to check a higher-level (meaning higher than Java), typed language and like Scala, even though to me there seems to be too much magic in it to overcome the non-dynamic aspect (this might not be formally correct, but hopefully understandable).

Due to this non-dynamic aspect, many things which are highly praised in Scala seem clumsy compared to Groovy. Just compare the granted-better-than-Java XML-handling in Scala with the even-much-better XML-handling in Groovy.

Of course, my IDE will never be able to code complete the Groovy variant – however, currently, the Scala Eclipse Plugin does it neither.

So, I bought the book “Programming in Scala”, which is good, but there are certain parts which are not too well described, one being the difference between “receive” and “react”.

I did it understand only after trying out myself (see below) and reading this post.

Once again, stackoverflow.com has been very useful to me (I just know the site for about 2-3 months and found useful information there several times).

So, I wanted to try if I can validate the different behaviour (which is in short that receive blocks a Thread and react returns immediately and is not bound to a particular Thread – but there are better explanations out there).

Preparation

I wanted to check the behaviour by forcing Scala to instantiate just one thread in the thread pool. This can be done by setting two system properties:

-Dactors.maxPoolSize=1 -Dactors.corePoolSize=1

this can be concluded by the Actors sources.

Running

I used two different objects:

import scala.actors._ import scala.actors.Actor._ object Reactor extends Actor { def act() { while(true) { receive { case (x: String, actor: Actor) => println("received:" + x) actor ! (x, self) } } } }

and the Runner:

import scala.actors._ import scala.actors.Actor._ object Runner extends Actor { def act() { while(true) { receive { case (string: String, actor: Actor) => println("got: " + string) actor ! ("hello", self) } } } def main(args : Array[String]) : Unit = { Runner.start() Reactor.start() Reactor ! ("hello", Runner) } }

Starting the application with this parameters just freezes it immediately. We need at least a threadpool of two threads. With these two threads, the application starts and the result with visualvm is like this:

Switching while(true) to loop and receive to react results in one sharing thread, which means the size can be max=1, core=1 and the application runs like this:

Which look like it should – the react does not block the thread, therefore one “shared” thread is enough – the react of each actor is then “attached” to the main thread.