Executor Service setting the flag to stop the thread

I am running simple thread which has run method as follows

public run()
while(!stopFlag){
   // print something Line 1
   // print something Line 2
   // print something Line 3
   // print something Line 4
}

If I run this thread through ExecutorService viz

ExecutorService exs = Executors.newFixedThreadPool(5);
exs.execute(new MyThread));

I stop the ExecutorService

exs.shutdown();

But this does not stop the thread as flag is not set to false. In another question related to same topic I was asked to properly handle InterruptedException which is caused when exs.shutdown() is called.
But in this case I am not doing any action that can throw InterruptedException.

What is the standard way to handle such case ?

Further question
Answer given by Sabir says “If your runnable doesn’t respond well to interrupts, nothing can be done to stop it other than shutting down the JVM. “.This seems to be my case.

But how to introduce handling of InterruptedException; if I am not calling any method that throws interrupted exception?

Leave a Reply

avatar
  Subscribe  
Notify of