Suppose I have a function, which invokes a blocking interruptible operation. I would like to run it asynchronously with a timeout. That is, I would like to interrupt the function when the timeout is expired.
So I am trying to do something like that:
import scala.util.Try import scala.concurrent.Future def launch(f: () => Unit, timeout: Int): Future[Try[Unit]] = { val aref = new java.util.concurrent.atomic.AtomicReference[Thread]() import ExecutionContext.Implicits.global Future {Thread.sleep(timeout); aref.get().interrupt} // 1 Future {aref.set(Thread.currentThread); Try(f())} // 2 }
The problem is that aref
in (1) can be null because (2) has not set it to the current thread yet. In this case I would like to wait until aref
is set. What is the best way to do that ?
You can go for a slightly easier approach using Await. The Await.result
method takes timeout duration as a second parameter and throws a TimeoutException
on timeout.
try {
import scala.concurrent.duration._
Await.result(aref, 10 seconds);
} catch {
case e: TimeoutException => // whatever you want to do.
}