Note that there are some explanatory texts on larger screens.

plurals
  1. POTransition from thread model to actors
    text
    copied!<p>Trying to get a handle on how to think in terms of actors instead of threads. I'm a bit stumped on the following use case:</p> <blockquote> <p>Consider a system that has a producer process that creates work (e.g. by reading data from a file), and a number of worker processes that consume the work (e.g. by parsing the data and writing it to a database). The rates at which work is produced and consumed can vary, and the system should remain robust to this. For example, if the workers can't keep up, the producer should detect this and eventually slow down or wait.</p> </blockquote> <p>This is pretty easy to implement with threads:</p> <pre><code>val producer:Iterator[Work] = createProducer() val queue = new LinkedBlockingQueue[Work](QUEUE_SIZE) val workers = (0 until NUM_WORKERS) map { i =&gt; new Thread() { override def run() = { while (true) { try { // take next unit of work, waiting if necessary val work = queue.take() process(work) } catch { case e:InterruptedException =&gt; return } } } } } // start the workers workers.foreach(_.start()) while (producer.hasNext) { val work = producer.next() // add new unit of work, waiting if necessary queue.put(work) } while (!queue.isEmpty) { // wait until queue is drained queue.wait() } // stop the workers workers.foreach(_.interrupt()) </code></pre> <p>There's nothing really wrong with this model, and I've successfully used it before. This example is probably overly verbose, as using an Executor or CompletionService would fit this task well. But I like the actor abstraction, and think it's easier to reason about in many cases. Is there a way to rewrite this example using actors, especially making sure that there are no buffer overflows (e.g. full mailboxes, dropped messages, etc)?</p>
 

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