Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need to peek ahead. Just keep track of the fact that an exit has been requested and use a reactWithin(0) to determine when the queue is empty after an exit has been requested.</p> <pre><code>import scala.actors._ sealed case class Message case object Exit extends Message case class Unimportant(n:Int) extends Message case class Important(n:Int) extends Message class SafeExitingActor extends Actor { def act : Nothing = react { case Exit =&gt; { println("exit requested, clearing the queue") exitRequested } case message =&gt; { processMessage(message, false) act } } // reactWithin(0) gives a TIMEOUT as soon as the mailbox is empty def exitRequested : Nothing = reactWithin(0) { case Exit =&gt; { println("extra exit requested, ignoring") exitRequested // already know about the exit, keep processing } case TIMEOUT =&gt; { println("timeout, queue is empty, shutting down") exit // TIMEOUT so nothing more to process, we can shut down } case message =&gt; { processMessage(message, true) exitRequested } } // process is a separate method to avoid duplicating in act and exitRequested def processMessage(message : Any, importantOnly : Boolean) = { message match { case Unimportant(n) if !importantOnly =&gt; println("Unimportant " + n) case Unimportant(n) =&gt; () // do nothing case Important(n) =&gt; println("Important! " + n) } Thread sleep 100 // sleep a little to ensure mailbox backlog } } object TestProcessing { def main(args : Array[String]) { val actor = new SafeExitingActor() actor.start() for (i &lt;- 1 to 10) { actor ! Unimportant(i) actor ! Important(i) } actor ! Exit for (i &lt;- 11 to 20) { actor ! Unimportant(i) actor ! Important(i) } actor ! Exit actor ! Important(100) } } </code></pre> <p>That should output</p> <pre><code>Unimportant 1 Important! 1 Unimportant 2 Important! 2 Unimportant 3 Important! 3 Unimportant 4 Important! 4 Unimportant 5 Important! 5 Unimportant 6 Important! 6 Unimportant 7 Important! 7 Unimportant 8 Important! 8 Unimportant 9 Important! 9 Unimportant 10 Important! 10 exit requested, clearing the queue Important! 11 Important! 12 Important! 13 Important! 14 Important! 15 Important! 16 Important! 17 Important! 18 Important! 19 Important! 20 extra exit requested, ignoring Important! 100 timeout, queue is empty, shutting down </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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