Kill or timeout a Future in Scala 2.10

廉价感情. 提交于 2019-11-30 18:50:54

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
}

No - you will have to add a flag that your loop checks. If the flag is set, stop the loop. Make sure the flag is at least volatile.

See Java Concurrency in Practice, p 135-137.

I had a similar problem and wrote the following nonblocking future op:

class TerminationToken(var isTerminated: Boolean)
object TerminationToken { def apply() = new TerminationToken(false) }

 implicit class FutureOps[T](future: Future[Option[T]]) {
 def terminate(timeout: FiniteDuration, token: TerminationToken): Future[Option[T]] = {
   val timeoutFuture = after[Option[T]](timeout, using = context.system.scheduler) {
     Future[Option[T]] { token.isTerminated = true; None } }
          Future.firstCompletedOf[Option[T]](Seq (future recover { case _ => None }, timeoutFuture))
     }
   }

Then just create a future that returns an option, and use .terminate(timeout, token) on it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!