Created
August 19, 2013 07:10
-
-
Save sandys/6266409 to your computer and use it in GitHub Desktop.
using Threads and runnables in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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