Kill or timeout a Future in Scala 2.10

前端 未结 3 1914
失恋的感觉
失恋的感觉 2021-01-04 20:11

Hi,

I\'m using Scala 2.10 with the new futures library and I\'m trying to write some code to test an infinite loop. I use a scala.concurrent.Future to r

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 20:16

    Do not try it at home.

    import scala.concurrent._
    import scala.concurrent.duration._
    
    class MyCustomExecutionContext extends AnyRef with ExecutionContext {
      import ExecutionContext.Implicits.global
      @volatile var lastThread: Option[Thread] = None
      override def execute(runnable: Runnable): Unit = {
        ExecutionContext.Implicits.global.execute(new Runnable() {
          override def run() {
            lastThread = Some(Thread.currentThread)
            runnable.run()
          }
        })
      }
      override def reportFailure(t: Throwable): Unit = ???
    }    
    
    implicit val exec = new MyCustomExecutionContext()
    val f = future[Int]{ do{}while(true); 1 }
    try {
      Await.result(f, 10 seconds) // 100% cpu here
    } catch {
      case e: TimeoutException => 
        println("Stopping...")
        exec.lastThread.getOrElse(throw new RuntimeException("Not started"))
          .stop() // 0% cpu here
    }
    

提交回复
热议问题