import scala.actors.threadpool.Executors
import scala.collection.concurrent.TrieMap
import scala.actors.threadpool.ExecutorService
object ThreadAffinity {
  val es = {
    val nbCores = Runtime.getRuntime.availableProcessors
    val execList = for(i <- 0 until nbCores) yield Executors.newSingleThreadExecutor
    val execStr = for(x <- Stream.continually(); y <- execList) yield y
    execStr.iterator
  }
  
  val idExecMap = TrieMap[Int, ExecutorService]()
  
  def exec(taskId: Int, task: Runnable) = idExecMap.getOrElseUpdate(taskId, es.next).execute(task)
}
I like the use of the "continually stream" that iterates over the list of pre-created threads.
I also like the very handy getOrElseUpdate from TrieMap
File generation with SBT
11 years ago
No comments:
Post a Comment