Note that there are some explanatory texts on larger screens.

plurals
  1. POScala actors: receive vs react
    primarykey
    data
    text
    <p>Let me first say that I have quite a lot of Java experience, but have only recently become interested in functional languages. Recently I've started looking at Scala, which seems like a very nice language.</p> <p>However, I've been reading about Scala's Actor framework in <a href="http://www.artima.com/shop/programming_in_scala" rel="noreferrer"><em>Programming in Scala</em></a>, and there's one thing I don't understand. In chapter 30.4 it says that using <code>react</code> instead of <code>receive</code> makes it possible to re-use threads, which is good for performance, since threads are expensive in the JVM.</p> <p>Does this mean that, as long as I remember to call <code>react</code> instead of <code>receive</code>, I can start as many Actors as I like? Before discovering Scala, I've been playing with Erlang, and the author of <a href="http://www.pragprog.com/titles/jaerlang/programming-erlang" rel="noreferrer"><em>Programming Erlang</em></a> boasts about spawning over 200,000 processes without breaking a sweat. I'd hate to do that with Java threads. What kind of limits am I looking at in Scala as compared to Erlang (and Java)?</p> <p>Also, how does this thread re-use work in Scala? Let's assume, for simplicity, that I have only one thread. Will all the actors that I start run sequentially in this thread, or will some sort of task-switching take place? For example, if I start two actors that ping-pong messages to each other, will I risk deadlock if they're started in the same thread?</p> <p>According to <em>Programming in Scala</em>, writing actors to use <code>react</code> is more difficult than with <code>receive</code>. This sounds plausible, since <code>react</code> doesn't return. However, the book goes on to show how you can put a <code>react</code> inside a loop using <code>Actor.loop</code>. As a result, you get</p> <pre><code>loop { react { ... } } </code></pre> <p>which, to me, seems pretty similar to</p> <pre><code>while (true) { receive { ... } } </code></pre> <p>which is used earlier in the book. Still, the book says that "in practice, programs will need at least a few <code>receive</code>'s". So what am I missing here? What can <code>receive</code> do that <code>react</code> cannot, besides return? And why do I care?</p> <p>Finally, coming to the core of what I don't understand: the book keeps mentioning how using <code>react</code> makes it possible to discard the call stack to re-use the thread. How does that work? Why is it necessary to discard the call stack? And why can the call stack be discarded when a function terminates by throwing an exception (<code>react</code>), but not when it terminates by returning (<code>receive</code>)?</p> <p>I have the impression that <em>Programming in Scala</em> has been glossing over some of the key issues here, which is a shame, because otherwise it's a truly excellent book.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload