How to exit the program correctly when using Scalaz frames and time functions

This works as expected:

object Planexecutor extends App {    
  import scalaz.concurrent.Future
  import scala.concurrent.duration._

  val f = Future.apply(longComputation)
  val result = f.run
  println(result)
}

It does not mean:

object Planexecutor extends App {    
  import scalaz.concurrent.Future
  import scala.concurrent.duration._

  val f = Future.apply(longComputation).timed(1.second)
  val result = f.run
  println(result)
}

In the first case, the application exits normally, while in the second case it is not. However, both versions correctly print the result value.

Is this a mistake or something I don’t understand?

+4
source share
1 answer

threadpool, timed. , , Strategy.DefaultTimeoutScheduler, java threadpool . Future.apply , , JVM . , :

  scalaz.concurrent.Strategy.DefaultTimeoutScheduler.shutdown()

:

  val newTimeOutScheduler = Executors.newScheduledThreadPool(1, new ThreadFactory {
    val defaultThreadFactory = Executors.defaultThreadFactory()
    def newThread(r: Runnable) = {
      val t = defaultThreadFactory.newThread(r)
      t.setDaemon(true)
      t
    }
  })

  val f = Future.apply(longComputation).timed(1.second)(newTimeOutScheduler)

implicits, :

  implicit val newTimeOutScheduler = Executors.newScheduledThreadPool(1, new ThreadFactory {
    val defaultThreadFactory = Executors.defaultThreadFactory()
    def newThread(r: Runnable) = {
      val t = defaultThreadFactory.newThread(r)
      t.setDaemon(true)
      t
    }
  })

  val f = Future.apply(longComputation).timed(1.second)

UPDATE: , .

+5

All Articles