Skip to content

Instantly share code, notes, and snippets.

@sandys
Created August 19, 2013 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandys/6266409 to your computer and use it in GitHub Desktop.
Save sandys/6266409 to your computer and use it in GitHub Desktop.
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")}
}).start
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