I have codebase that calls a black box, that in turn calls other code I have control of, call it thing.innerMethod()
. The code in the black box needs to execute in a different thread than the code that calls it, so I have decided to use an executor:
Future<String> bbFuture = executorService.submit(() -> {
return blackBox.doStuff(thing);});
If the execution takes too long, the user might call another endpoint that ultimately calls bbFuture.cancel()
, triggering a CancellationException
in the thread with the future and an InterruptedException
in the thread running inside the executor.
The problem is that when the InterruptedException
propagates into the black box, it catches it and logs it a stack trace, and raises false alarms. I don't need to catch every InterruptedException
, but I know a place I could put a catch
that would get probably 90% of them. The problem is I don't really have a good way to stop execution without returning from the function, and any partial or dummy result would probably trigger another exception. I know I could use Thread.currentThread().stop()
, but stop()
is deprecated.
How can I stop execution inside a java executor without returning or throwing an exception?