Background task in Scala Background task in Scala multithreading multithreading

Background task in Scala


There are many ways to do that, but I would do something simple like the following.

import scala.concurrent.ops._spawn {  while (true) {     Thread.sleep(1000);    // clear the cache's old entries  }}

Hope this helps.


You could use Akka Scheduler, which allows you to send a reccuring message to an (akka) actor doing the job. From the doc, just use:

import akka.actor.Scheduler//Sends messageToBeSent to receiverActor after initialDelayBeforeSending and then after each delayBetweenMessagesScheduler.schedule(receiverActor, messageToBeSent, initialDelayBeforeSending, delayBetweenMessages, timeUnit)


Futures is a simple way to do it without explicitly starting a new thread

import scala.actors.Futures._// main thread code herefuture {   // second thread code here}// main thread code here