Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Created September 22, 2014 20:27
Show Gist options
  • Save djspiewak/f758b7f9d0e55a7f0e5e to your computer and use it in GitHub Desktop.
Save djspiewak/f758b7f9d0e55a7f0e5e to your computer and use it in GitHub Desktop.
import scalaz._
import concurrent._
// utility for shifting off to a different thread
def thread[A](body: => A)(cb: A => Unit): Unit = {
val t = new Thread {
setName("test-thread")
override def run(): Unit = cb(body)
}
t.start()
}
// shift to a new thread and return as an async Task
def threadTask[A](body: => A): Task[A] = Task async { cb => thread(body) { a => cb(\/-(a)) } }
// show that we're actually shifting off into a different thread
val name1 = threadTask { Thread.currentThread.getName } run
// shift to a different thread, then fork back into the default pool and check the thread name
val name2 = Task fork (threadTask { () }) >> (Task delay { Thread.currentThread.getName }) run
name1 == name2 // => true; uh, why?!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment