Skip to content

Instantly share code, notes, and snippets.

@sandys sandys/threads.scala
Created Aug 19, 2013

What would you like to do?
using Threads and runnables in Scala
package multi
import java.util.concurrent.Callable
object Threads {
def main(args: Array[String]) : Unit = {
implicit def funcToCallable[A](func : () => A): Callable[A] = new Callable[A] { println("creating callable");def call() :A = func() }
implicit def funcToRunnable( func : () => Unit ) = new Runnable(){ println("creating runnable");def run() = func()}
//bad. cannot be tested properly. plus completely screws up the idea of runnable (viz to have the choice of not using threads with minimum code changes)
new Thread(new Runnable{
def run {println("hi")}
def run2() = {println("hi2")}
//good - uses implicit conversions
new Thread( () => run2).start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.