0
我正在使用sbt 0.13.9和scalatest。該項目包含一堆守護進程線程。簡化示例:如何在sbt shell中運行測試後關閉線程
object Storage {
private lazy val scheduler = Executors.newSingleThreadScheduledExecutor(
new com.twitter.concurrent.NamedPoolThreadFactoryNamedPoolThreadFactory("thread-name", true)
)
scheduler.scheduleAtFixedRate(new Runnable {
override def run(): Unit = {
println("Running ...")
// do something
}
}, 0, 1, TimeUnit.MINUTES)
// other code
}
一些測試使用Storage
。在SBT shell中運行test
時,可以看到Runnable
在所有測試完成後仍在運行。
[info] All tests passed.
[success] Total time: 32 s, completed Dec 1, 2015 10:39:57 AM
> 2015-12-01 10:40:51,806 INFO Storage$$anon$1 - Running ...
2015-12-01 10:41:51,810 INFO Storage$$anon$1 - Running ...
我該如何確保它們被停止?
如果您發送'true'作爲秒參數,那麼已經在'NamedPoolThreadFactory'中完成了。 – reikje
我已閱讀我的答案。 – Rumoku
調度程序是從測試線程創建的。測試實例化一個包含調度器的對象。我無法取消'ScheduledFuture',因爲這是一個循環任務。 – reikje