Java alternative Thread.stop () to interrupt a specific call

I'm looking for a way to tell this call, which takes about 20-120 seconds:

final Area image = ...
final AffineTransform transform = new AffineTransform();
transform.scale...
image.transform(transform); //this specific line takes that long

      

to stop if Thread.interrupt () is called;

Because I just want to call this one time, I cannot run some things after a while (Thread.currentThread.isInterrupted ()) and throw InterruptedException. I can run if isNotInterrupted before and after the call, but still, how do I stop this line of code?

+3


source to share


1 answer


Interrupting threads in Java is collaborative.

But I think you understand that. If your affine transformation takes a long time and you try to run it in one go without even checking the interrupted flag, your transformation will successfully convert.

No Thread.kill()

.

You have no choice. Note that I don't know if this is possible, but ideally such a long streaming stream should be spliced ​​into manageable timing chunks and the interrupt flag is checked between those chunks.



Of course, you also have the option not to run this transformation on the current thread, but instead ExecutorService

, and grab the Future

result; and you will be .get()

timed out.

But the problem of the end will remain: you MUST check the interrupt flag. And you have to deal with TimeoutException

on .get()

.


Okay, I suck on math. But can't this transformation be split across multiple threads? Can't you use a fork / join structure for example? Or even better if you are using Java 8, Stream

and Collector

?

+2


source







All Articles