What the right way to interrupt executor’s thread?
I’ve got this:
Thread class with name Worker with method:
public void run() {
while(!(Thread.currentThread().isInterrupted()){
System.out.println("work " + Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted());
}
}
And main class with:
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
Worker worker = new Worker();
executorService.execute(worker);
I try to call worker.interrupt(); or executorService.shutdownNow(); but my thread goes on and isInterrupted() is false.
Can you post all the relevant code? Based on the information you have given, I can’t reproduce the behaviour you describe. See below a SSCCE that works as expected – output:
Code: