Comments on Programming Scalaurn:uuid:70ca54ca-2942-3ef4-be13-bea215cf48562009-08-11T20:58:53-07:00 <div>Actors in Scala <p id="para_after_opening_the_shop_we_g">After &#8220;opening the shop&#8221;, we generate a number of <code class="literal">Customer</code> objects, assigning a numeric ID to each and storing the lot in an <code class="literal">ArrayBuffer</code>. Next, we &#8220;trickle&#8221; the customers in by sending them as messages to the shop and sleeping for a semi-random amount of time between loops. At the end of our simulated day, we tally up the number of customers who got haircuts by filtering out the customers whose internal <code class="literal">shorn</code> boolean was set to <code class="literal">true</code> and asking for the size of the resulting sequence.</p> <blockquote><p>One other non-obvious issue: your Barbershop simulator and Barber could very well be using the exact same stream of random numbers if they both call Random() within 1ms of each other. It probably shouldn't give you adverse behavior in this example, but imagine you added "hair length" to your Customers which was determined by a random number generator at construction: all customers would have identical hair length. Even if you manually specified seeds (or there was more than 1ms delay), there could still be dependencies due to artifacts related to Java's weak random number generation (see my example at https://lampsvn.epfl.ch/trac/scala/ticket/2149). I'd like to see Scala references and docs make some warning about being careful with random number generation in concurrent programs. </p> &#8212; joebot (2009-07-13 11:56:07) </blockquote> </div>urn:uuid:0e5a2d7b-cc9a-3861-980d-5d50e35c4533joebot2009-07-13T11:56:07-07:00Comment on 'Actors in Scala' <div>Actors in Scala <p id="para_lastly_know_when_actors_aren_">Lastly, know when Actors aren&#8217;t appropriate. Just because Actors are a great way to handle concurrency in Scala doesn&#8217;t mean they&#8217;re the <span class="emphasis"><em>only</em></span> way, as we&#8217;ll see below. Traditional threading and locking may better suit write-heavy critical paths for which a messaging approach would incur too much overhead. In our experience, you can use a purely Actor-based design to prototype a concurrent solution, then use profiling tools to suss out parts of your application that might benefit from a different approach.</p> <blockquote><p>Will consider adding. </p> &#8212; deanwampler (2009-07-12 21:55:32) </blockquote> </div>urn:uuid:5f671a29-7c29-350f-a490-41e462678e59deanwampler2009-07-12T21:55:32-07:00Comment on 'Actors in Scala' <div>Actors in Scala <p id="para_after_opening_the_shop_we_g">After &#8220;opening the shop&#8221;, we generate a number of <code class="literal">Customer</code> objects, assigning a numeric ID to each and storing the lot in an <code class="literal">ArrayBuffer</code>. Next, we &#8220;trickle&#8221; the customers in by sending them as messages to the shop and sleeping for a semi-random amount of time between loops. At the end of our simulated day, we tally up the number of customers who got haircuts by filtering out the customers whose internal <code class="literal">shorn</code> boolean was set to <code class="literal">true</code> and asking for the size of the resulting sequence.</p> <blockquote><p>Thanks for the feedback. Obviously, we're trying to strike a balance between keeping it simple for clarity, yet realistic enough to be applicable to nontrivial problems. Will consider what we can do here. </p> &#8212; deanwampler (2009-07-12 21:55:02) </blockquote> </div>urn:uuid:540afa81-718f-398e-b8af-bedece0000d4deanwampler2009-07-12T21:55:02-07:00Comment on 'Actors in Scala' <div>Actors in Scala <p id="para_strait_away_we_see_several_ob">The sleeping barber problem is usually solved with semaphores and mutexes, but we&#8217;ve got better tools at our disposal. Straightaway, we see several things to model as Actors: the barber is clearly one, as are the customers. The barbershop itself could be modeled as an Actor, too; there need not be a real-world parallel to verbal communication in an Actor system, even though we&#8217;re sending messages.</p> <blockquote><p>Thanks. will fix </p> &#8212; deanwampler (2009-07-12 21:53:51) </blockquote> </div>urn:uuid:9f5c5dd1-331d-3db2-aa68-4621d171d196deanwampler2009-07-12T21:53:51-07:00Comment on 'Actors in Scala' <div>Actors in Scala <p id="para_lastly_know_when_actors_aren_">Lastly, know when Actors aren&#8217;t appropriate. Just because Actors are a great way to handle concurrency in Scala doesn&#8217;t mean they&#8217;re the <span class="emphasis"><em>only</em></span> way, as we&#8217;ll see below. Traditional threading and locking may better suit write-heavy critical paths for which a messaging approach would incur too much overhead. In our experience, you can use a purely Actor-based design to prototype a concurrent solution, then use profiling tools to suss out parts of your application that might benefit from a different approach.</p> <blockquote><p>Some info on using Actors and futures (i.e. "!!") would be helpful </p> &#8212; joebot (2009-07-02 15:03:07) </blockquote> </div>urn:uuid:1675a31b-5a55-3839-9d19-2b5f2e610030joebot2009-07-02T15:03:07-07:00Comment on 'Actors in Scala' <div>Actors in Scala <p id="para_after_opening_the_shop_we_g">After &#8220;opening the shop&#8221;, we generate a number of <code class="literal">Customer</code> objects, assigning a numeric ID to each and storing the lot in an <code class="literal">ArrayBuffer</code>. Next, we &#8220;trickle&#8221; the customers in by sending them as messages to the shop and sleeping for a semi-random amount of time between loops. At the end of our simulated day, we tally up the number of customers who got haircuts by filtering out the customers whose internal <code class="literal">shorn</code> boolean was set to <code class="literal">true</code> and asking for the size of the resulting sequence.</p> <blockquote><p>I think this is a great example and a great toy problem! However, I find Thread.sleep(2000) to be a slightly unsatisfying way to wait for concurrent actions to complete. It's difficult to guarantee that all computation finishes by a certain time, especially if you were to use RemoteActors. This also skews any benchmarking you might want to do. Knowing how to end a concurrent problem gracefully is essential to many applications, although it's probably overkill for this example.<br /> </p> <p>I like to have a definitive signal or message that computation has finished. I have gone about this by having the actors communicate with the coordinating class (which isn't explicitly an actor itself). I'm not sure if this is the best way to do things. </p> &#8212; joebot (2009-07-02 14:57:11) </blockquote> </div>urn:uuid:a22c22a7-c2f4-32b4-b07f-a572ebbd560fjoebot2009-07-02T14:57:11-07:00Comment on 'Actors in Scala' <div>Actors in Scala <p id="para_strait_away_we_see_several_ob">The sleeping barber problem is usually solved with semaphores and mutexes, but we&#8217;ve got better tools at our disposal. Straightaway, we see several things to model as Actors: the barber is clearly one, as are the customers. The barbershop itself could be modeled as an Actor, too; there need not be a real-world parallel to verbal communication in an Actor system, even though we&#8217;re sending messages.</p> <blockquote><p>Typo: "Actosr" </p> &#8212; joebot (2009-07-02 13:42:27) </blockquote> </div>urn:uuid:bcde6274-0e9c-3993-98db-ce50730e7092joebot2009-07-02T13:42:27-07:00Comment on 'Actors in Scala'