简体   繁体   中英

Stopping a thread from another thread in a hashmap using a volatile boolean flag in java

So i have my MainThread class that is responsible for all other threads.(creating, stopping, monitoring)

 public class MainThread implements Runnable {
      public static volatile boolean keepRunning = true;  

      public void run(){


      //some code..


      while(keepRunning){

      //here i am creating threads for my Wired class (explaining below) 
      //that are stored in a hashmap

      Wired wiredInterface = new Wired(s,defaultSleepTime,c,max,percent);
      Thread t1 = new Thread(wiredInterface);
      t1.start();
      }

far down in my code there is a case where i need to stop the thread t1.

My Wired Class:

   public class Wired implements Runnable {
        private static volatile boolean keepRunning = true;

        public void run(){

        while(keepRunning){

        //code

        try{
            Thread.sleep(((k - i + 1)*defaultTime) - DT);  //just to show that this thread sleeps periodically. doesnt affect the question
        }catch(InterruptedException e){e.printStackTrace();}  

        }
        }

In my Wired class i have this method for changing the volatile flag.

        public static void stopRunning(){
            keepRunning = false;
        }

My Question is..how can i access the method stopRunning from my MainThread for the specific thread that i want to stop? Thread.interrupt() doesnt work for me as a solution.

I have looked at a lot of similar questions referring to this subject but i havent found something suitable for my case. Sorry if i missed something This code is an oversimplification of my actual code

You should make keepRunning a instance variable (attribute) instead of static.

Whenever you want stop a Thread, grab it from Map and set the attribute keepRunning to false using setKeepRunning(false).

Don't reinvent the wheel here. Use Thread.interrupt(), and properly check the Thread.isInterrupted() flag, and/or properly handle InterruptedException. IE don't swallow it or printStackTrace() it and continue. If you receive an InterruptedException catch it at the borders of the Runanble.run() method and stop your outer loop and shutdown the thread.

Your method should be changed to this:

public void run() {
   try {
      while( !Thread.currentThread().isInterrupted() ) {
          doSomeThreadedThing();
      }
   } catch( InterruptedException e ) {
      // maybe log some info you are shutting down
   }
}

It's really simple to properly shutdown a thread provided it isn't stuck in some IO. If you have a long running task that you don't want to wait for check Thread.isInterrupted() within your logic periodically. There are no advantages provided by your volatile boolean flag mechanism you have shown over using Thread.interrupted().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM