Note that there are some explanatory texts on larger screens.

plurals
  1. POGracefully terminating a Scala program after using Actor.self
    primarykey
    data
    text
    <p>Using <em>scala.actors.Actor.self</em> creates a <em>ForkJoinScheduler</em>, which sticks around and keeps the program from terminating normally. I'd like to use Scala actors within a program that is written in Java, and currently uses a thread pool which shuts down gracefully. It would be ideal if I could coerce <em>Actor</em> to use a <em>DaemonScheduler</em>, but if need be I'd be happy to issue an explicit termination command.</p> <p>Here's some sample code:</p> <pre><code>object EchoActor extends DaemonActor { def act() { loop { react { case (x: Any, respondTo: Actor) =&gt; { println("echoActor Got message " + x) respondTo ! "Echoing: " + x } case msg =&gt; println("Can't handle message " + msg) } } } } object TestRunner extends App { EchoActor.start() for (n &lt;- 1 to 3) { EchoActor !("Time to echo", self) println(self.receiveWithin(1000) { case x =&gt; x}) Actor.clearSelf() // &lt;&lt;-- This doesn't allow for graceful shutdown } Actor.resetProxy() // &lt;&lt;-- Neither does this println("Done.") } </code></pre> <p>This program doesn't terminate, even though there are no threads left that could use <em>Actor.self</em>. I've tried calling <em>Actor.exit()</em>, which just throws an <em>InterruptedException</em>, and I'm just about to give up completely on <em>Actor.self</em>. Am I missing something?</p> <p><strong>Update</strong></p> <p>Just discovered:</p> <pre><code>scala.actorsScheduler.shutdown() </code></pre> <p>which works for global termination, and will get the job done for now. I would prefer to change the default scheduler to be a <em>DaemonScheduler</em>, so please post an answer if you know how.</p>
    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. 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