enter image description here Threads are not stopped even after calling Interruption from Executors shutdownNow method.
Thread Call functionality is running in a while loop , which is checking for Interruption flag.
I tried sending Interruption flag to the running thread after a certain period, but it is still executing.I want to force stop the thread.
Can anybody tell this behavior.
Attaching the sample Java code:
public class TestExecutor {
static volatile int j = 1;
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(5);
for (int i = 1; i <= 10; ++i) {
Future<Map<String, List<String>>> abc = pool.submit(new Callable<Map<String, List<String>>>() {
volatile boolean abortFlag = false;
@Override
public Map<String, List<String>> call() throws Exception {
while(!abortFlag){
long start = System.currentTimeMillis();
for(int k=0; k < 10e4 ;k++){
abortFlag = abort();
System.out.println(Thread.currentThread().getName() +" " +abortFlag);
}
System.out.println("counter val is:" +Thread.currentThread().getName() +" : " +j++);
long end = System.currentTimeMillis();
System.out.println("time for one execution : " +" " +Thread.currentThread().getName() +" :" +(end-start));
return null;
}
return null;
}
private boolean abort() {
if(Thread.currentThread().isInterrupted()){
return true;
}else{
return false;
}
}
});
}
pool.shutdown();
try {
if (pool.awaitTermination(3000, TimeUnit.MILLISECONDS)) {
System.out.println("task completed");
} else {
System.out.println("Forcing shutdown...");
pool.shutdownNow();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Closed");
}
} `