Created
July 28, 2015 05:18
-
-
Save tkawachi/9ab262c208e12e15ae8f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.concurrent.atomic.AtomicInteger | |
import java.util.concurrent.{ ThreadFactory, Executors, ExecutorService } | |
import scalaz.concurrent.Task | |
object Test { | |
class MyThreadFactory(prefix: String) extends ThreadFactory { | |
val n = new AtomicInteger(0) | |
override def newThread(r: Runnable) = { | |
val t = Executors.defaultThreadFactory().newThread(r) | |
val threadN = n.incrementAndGet() | |
t.setName(s"$prefix-$n") | |
t.setDaemon(true) | |
t | |
} | |
} | |
val serviceA = Executors.newFixedThreadPool(1, new MyThreadFactory("serviceA")) | |
val serviceB = Executors.newFixedThreadPool(1, new MyThreadFactory("serviceB")) | |
def main(args: Array[String]): Unit = { | |
val printThreadTask = Task(println(Thread.currentThread))(serviceA) | |
val forkedTask = Task.fork(printThreadTask)(serviceB) | |
println("printThreadTask.run") | |
printThreadTask.run | |
println("forkedTask.run") | |
forkedTask.run | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment