This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Here's how to initialize Actor.self to be a DaemonActor

3 replies
wrburdick
Joined: 2009-09-21,
User offline. Last seen 3 years 3 weeks ago.
I wanted this so I could use actor stuff in my main method and still have the program shut down properly, but there wasn't an explicit API for this.  In case anyone out there needs this, also, here's the "exploit".  Just put this line in your code, before Actor.self gets called for the first time:
new scala.actors.DaemonActor {def act() = {}} ! null
When you call it for a thread that doesn't have an actor associated with it, the automagically created ActorProxy gets the new DaemonActor's scheduler instead of the default Scheduler object.

Bill
Todd Vierling
Joined: 2011-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Here's how to initialize Actor.self to be a DaemonActor
On Wednesday, October 26, 2011 6:59:42 PM UTC-4, Bill Burdick wrote:
I wanted this so I could use actor stuff in my main method and still have the program shut down properly, but there wasn't an explicit API for this.

Weird. This doesn't do it?
scala.actors.Scheduler.impl = scala.actors.scheduler.DaemonScheduler
wrburdick
Joined: 2009-09-21,
User offline. Last seen 3 years 3 weeks ago.
Re: Re: Here's how to initialize Actor.self to be a DaemonActor
On Thu, Oct 27, 2011 at 1:13 PM, Todd Vierling <tv [at] duh [dot] org> wrote:
On Wednesday, October 26, 2011 6:59:42 PM UTC-4, Bill Burdick wrote:
I wanted this so I could use actor stuff in my main method and still have the program shut down properly, but there wasn't an explicit API for this.

Weird. This doesn't do it?
scala.actors.Scheduler.impl = scala.actors.scheduler.DaemonScheduler

Oh, I didn't see that variable.  Would that be a thread-safe approach to pick a scheduler?

Bill
Todd Vierling
Joined: 2011-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Here's how to initialize Actor.self to be a DaemonActor

On Thu, Oct 27, 2011 at 11:32 PM, Bill Burdick wrote:
>> Weird. This doesn't do it?
>> scala.actors.Scheduler.impl = scala.actors.scheduler.DaemonScheduler
>
> Oh, I didn't see that variable.

It's not technically a var; it's a simulated var through the existence
of explicitly defined impl() and impl_=(IScheduler) defs.

>  Would that be a thread-safe approach to pick a scheduler?

Not likely. AFAIK it's only usable to pick a default scheduler for the
runtime. I use Scheduler.impl_= to assign to a new
SingleThreadScheduler for use in an environment that doesn't allow for
spawned threads (this can be used with Reactors but not traditional
Actors).

I have _not_ tried it with DaemonScheduler, but that thought occurred
to me when I saw your post.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland