我想生成一个线程并在该线程中运行代码。Scala中有哪些选项?
示例用法如下所示:
Thread.currentThread setName "MyThread"
val myThreadExecutor = ???
val threadNamePromise = Promise[String]
future {
myThreadExecutor run {
val threadName = "MySpecialThread"
Thread.currentThread setName threadName
threadNamePromise success threadName
}
}
Await.result(threadNamePromise.future, Duration.Inf)
future {
myThreadExecutor run {
println(Thread.currentThread.getName) // MySpecialThread
}
}
future {
myThreadExecutor run {
println(Thread.currentThread.getName) // MySpecialThread
}
}
println(Thread.currentThread.getName) // MyThread
我可以使用内置的 Scala 库中的任何内容吗?
编辑
我更新了片段以更好地反映意图