import scala.sys.process._
import scala.concurrent._ // (future)
import ExecutionContext.Implicits.global // (future)
/* run "ls /opt"
* which outputs 2 lines:
* - "X11"
* - "local"
*/
val command = Process(Seq("ls", "/opt"), None, "LANG" -> "")
val process = command.run(ProcessLogger(line => {
Thread.sleep(1000) // slow execution down
println(Thread.currentThread)
println(line)
println("")
}))
Future {
process.exitValue // blocks until the process exits
}.onComplete { results =>
println(Thread.currentThread)
println("process exited: after last line callback finished?")
println("DONE")
}
输出( 顺序预期 ,但是这总是这样?):
Thread[Thread-47,5,run-main-group-0]
LINE X11
Thread[Thread-47,5,run-main-group-0]
LINE local
Thread[ForkJoinPool-1-worker-3,5,run-main-group-0]
process exited: after last line callback finished?
DONE
因为它似乎确定onComplete
后线相关的输出完全是在块输出其文本。 但是,这始终是这样吗? 谁可以确保不同的线程中运行一个接一个? 我可以依靠这个所谓的订单?