How to run code in a separate thread?

Yes, there is. You can use the scala.concurrent from the standard library. More specifically, you can use futures - highly composable asynchronous computations.

import java.util.concurrent.Executors
import concurrent.{ExecutionContext, Await, Future}
import concurrent.duration._

object Main extends App {

  // single threaded execution context
  implicit val context = ExecutionContext.fromExecutor(Executors.newSingleThreadExecutor())

  val f = Future {
    println("Running asynchronously on another thread")
  }

  f.onComplete { _ =>
    println("Running when the future completes")
  }

  Await.ready(f, 5.seconds) // block synchronously for this future to complete

}

Futures run in an execution context, which is an abstraction for thread pools. This context can be passed implicitly. In the above example, we used a global one, defined by the Scala library - but you can control this aspect of your program by allocating many execution contexts.

The snippet only does what you asked - running code concurrently. However, futures are much more than that - they allow you to compute values asynchronously, compose multiple futures to obtain results with dependencies amongst them, or in parallel.

Here's an intro: http://docs.scala-lang.org/overviews/core/futures.html


Using the answer of @alexwriteshere as a basis I created this implementation:

import java.util.concurrent.Executors
import scala.concurrent.future
import scala.concurrent.JavaConversions.asExecutionContext

class ApplicationThread {
  protected implicit val context = 
    asExecutionContext(Executors.newSingleThreadExecutor())

  def run(code: => Unit) = future(code)
}

Update

Thanks to @Dth for pointing out that this is the modern version:

import java.util.concurrent.Executors
import scala.concurrent.{ExecutionContext, Future}

class ApplicationThread {
  protected implicit val context = 
    ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor())

  def run(code: => Unit) = Future(code)
}

Besides the standard concurrency library, there's a few more. For example, there's com.twitter / util-core library that lets you do:

val pool = FuturePool.unboundedPool
pool {
   <code>
}

Your example would look like:

Thread.currentThread setName "MyThread"

// because you have to be using a single-threaded one to get the same name
val pool = FuturePool(Executors.newSingleThreadExecutor())

pool {
  Thread.currentThread setName "MySpecialThread"
}

pool {
  println(Thread.currentThread.getName) // MySpecialThread
}

println(Thread.currentThread.getName)

Tags:

Scala