views:

133

answers:

2

AKA doing something at set intervals.

For example, let's say I want to scan a certain directory every 60 seconds.

In Java, I would use a ScheduledExecutorService like so:

Executor pool = Executors.newScheduledThreadPool(10)
pool.scheduleAtFixedRate(scanner, 0, 60, TimeUnit.SECONDS)

and that works fine.

The thing is, I'm thinking I'd like to try using Scala actors in my program, but I'm a little confused as to how to combine actors and Java Executors, or whether they should be.

I guess maybe I could have a simple runner which would merely send a message to an actor every N seconds -- does that make sense?

A: 

You could try the Scala Executor itself, instead of the Java one.

An object that executes submitted java.lang.Runnable tasks.
This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.
An Executor is normally used instead of explicitly creating threads. For example, rather than invoking new Thread(new(RunnableTask())).start() for each of a set of tasks, you might use:

 Executor executor = anExecutor;
 executor.execute(new RunnableTask1());
 executor.execute(new RunnableTask2());
 ...
VonC
Thanks, but that looks like it's just an interface. I'm having trouble finding this package in the official Scala 2.8 API doc nightlies.
Avi Flax
+1  A: 

I guess maybe I could have a simple runner which would merely send a message to an actor every N seconds -- does that make sense?

Yes, and consider using Akka for the Actors by the way. It has a simpler API, better performance and has a lot of yummy stuff in it.

olle kullberg
I'm definitely open to using Akka, but I'm a little perplexed... if actors are such an important part of Scala, and Akka's API and implementation are better, then why isn't it being integrated into Scala itself? I can only surmise that there's conflict in the community about this.
Avi Flax
The Actor impl. shipped with Scala is not really a part of the language, it just feels like that. Akka can not replace the Actor impl. shipped with Scala since they do not have the same API. Like Guy Steele would have said: You can't put every good framework into the language, better to have each user make the decision. Akka is a perfect example of how you scale (=extend) Scala, and Scala is made to scale. But there are plans for putting STM into Scala. If you do not want to wait for that you can use the STM in Akka.
olle kullberg
Thanks Olle, very helpful.But in fact, given that the default implementation is "just" a library, that makes me even more curious as to why not replace it with Akka?I guess I'm saying, what are the pros and cons? There must be some pros of the default implementation, or it wouldn't remain the default.
Avi Flax
Keeping Akka as it is, a framework standing on it's own, will give Jonas Bonér and Viktor Klang (creator and main contributor to Akka) the freedom they need to do with it as they see fit. There are great plans for Akka, and the framework has company backing. Shipping the framework with Scala (like they did with the first Actor implementation) might not be a very good idea. If Akka is embedded into Scala, some of this freedom will be lost. You can look at the Actor implementation you get for free with Scala as the reference J2EE implementation SUN provided (many years ago), i.e not very good.
olle kullberg
Just to chime in, here's a doc link to use the Scheduler in Akka: http://doc.akkasource.org/scheduler
Viktor Klang