I'm using a library that has a long-running RPC longRunningCall
. Right now I'm submitting Java futures to a thread pool like:
val myFuture = myPool.submit(() => { someLibrary.longRunningCall() })
Unfortunately longRunningCall
can hang indefinitely. I first tried waiting for the Future like:
Await.result(myFuture, Duration(30, TimeUnit.Seconds))
This works okay, but the future keeps running in the background, eating up threads in my thread pool.
I then tried catching the TimeoutException and calling myFuture.cancel(true)
, but it seems like longRunningCall
doesn't respect the interruption and keeps running. This means my thread pool eventually just fills up with zombie threads.
How can I run this task in a background thread and actually, forcibly, cancel it using Scala or Java?
I'm aware of many other threads on the topic, but there is an assumption in the answers I see that I have control over the long-running task, e.g.
Future task of ExecutorService not truly cancelling
How to cancel Future in Scala?
How to cancel Java 8 completable future?
As such I do not see a question that is truly a duplicate.
Is this just not possible?