Treat InterruptedException in CompletionService as task being cancelled

This commit is contained in:
Alex Zolotko 2020-02-25 11:50:45 +01:00 committed by Alex Zolotko
parent dca44d570d
commit 7a1a524b4e
1 changed files with 13 additions and 2 deletions

View File

@ -34,8 +34,19 @@ object CompletionService {
def take() = completion.take().get()
}
def submit[T](work: () => T, completion: JCompletionService[T]): () => T = {
val future = try completion.submit { new Callable[T] { def call = work() } } catch {
case _: RejectedExecutionException => throw Incomplete(None, message = Some("cancelled"))
val future = try completion.submit {
new Callable[T] {
def call =
try {
work()
} catch {
case _: InterruptedException =>
throw Incomplete(None, message = Some("cancelled"))
}
}
} catch {
case _: RejectedExecutionException =>
throw Incomplete(None, message = Some("cancelled"))
}
() => future.get()
}