Cancellation with Future and Promise in Scala

后端 未结 4 1389
情歌与酒
情歌与酒 2020-12-06 02:02

This is a followup to my previous question.

Suppose I have a task, which executes an interruptible blocking call. I would like to run it as a Future<

4条回答
  •  独厮守ぢ
    2020-12-06 02:53

    scala.concurrent.Future is read-only, so one reader cannot mess things up for the other readers.

    It seems like you should be able to implement what you want as follows:

    def cancellableFuture[T](fun: Future[T] => T)(implicit ex: ExecutionContext): (Future[T], () => Boolean) = {
      val p = Promise[T]()
      val f = p.future
      p tryCompleteWith Future(fun(f))
      (f, () => p.tryFailure(new CancellationException))
    }
    
    val (f, cancel) = cancellableFuture( future => {
      while(!future.isCompleted) continueCalculation // isCompleted acts as our interrupted-flag
    
      result  // when we're done, return some result
    })
    
    val wasCancelled = cancel() // cancels the Future (sets its result to be a CancellationException conditionally)
    

提交回复
热议问题