Similar to this question, but with difference is in the details: Interrupting a thread that waits on a blocking action?
There is a 3pty library that I don't control, this library has a method pressButtonAndHoldIt(long duration)
. This is a blocking method, meaning that if you set duration
to 10 minutes - it will be doing something for 10 minutes (without interruptions).
I use this method from a separate thread, so I can do something else while "holding the button":
final Future<?> f = Executors.newSingleThreadExecutor().submit(() -> {
3ptyLibrary.pressButtonAndHoldIt(Duration.ofMinutes(10).toSeconds());
});
var stuff = myOtherStuff();
//here I want to stop what was submitted in "f", e.g. "f.forceStop()"
Usually myOtherStuff()
takes about 9 minutes, but sometimes it could take 5 minutes, so I want to stop that blocking background process. How can I do that?
Note that Executors.newSingleThreadExecutor().submit()
is just an example: I am able to create a background thread in any way we can in java (if that helps).