Call Stop Thread method when JVM termiates

40 Views Asked by At

below is my Thread code. I want to call stopThread() method when I terminates JVM. Please suggest right way to do.

public class Test {
    public static void main(String[] args) {
            ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(10);
            for(int i=0;i<5;i++){
            scheduledExecutor.submit(new MyRunnable())
          }
         scheduledExecutor.shotdown()
        .....
}  

public class MyRunnable implements Runnable
{
      boolean isRunning = true;
      public void run()
      {
           while(isRunning)
          {
           Thread.sleep(5000);
         } 
      }
      public void stopThread()
      {
          isRunning = false;
      }
}
        
0

There are 0 best solutions below