I've tried looking on other threads and I can't seem to find a proper answer,
When using executorService.shutdownNow() almost all tasks stop immediately as intended except ones with for loops in them. I have no idea why it happens, but the most I've seen from other threads is that you should be using Thread.currentThread().isInterrupted() to check if it is interrupted, but adding a
if(Thread.currentThread().isInterrupted()) {return;}
Doesn't actually stop it all.
(I'm calling executor shutdownNow when a UI button is toggled off)
Code example:
executorService.submit(() -> {
ctx.batzUtils.keyEvent(KeyEvent.KEY_PRESSED, VK_SHIFT);
//Shit keeps going if you call ShutdownNow Todo
for (Rectangle rect : rects)
{
ctx.batzUtils.click(rect);
try
{
Thread.sleep(minDelayBetween, maxDelayBetween);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
ctx.batzUtils.keyEvent(KeyEvent.KEY_RELEASED, VK_SHIFT);
ctx.batzUtils.keyEvent(KeyEvent.KEY_TYPED, VK_SHIFT);
});
Any ideas as to why or solutions would be greatly appreciated
CodePudding user response:
2 solutions. your for loop you do need to check
if(Thread.currentThread().isInterrupted()) {[clean up and finish the thread code here]}
In your catch
section, instead of e.printStackTrace();
place [clean up and finish the thread code]. In the catch statement you don't need to check if the thread has been interrupted. The fact that you caught the InterruptedException is already a flag that the thread has been interrupted
CodePudding user response:
Adding Thread.currentThread().interrupt() in my catch statement worked
Credit To @Sambit