简体   繁体   English

使用java中的volatile boolean标志从hashmap中的另一个线程停止一个线程

[英]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) 所以我有我的MainThread类负责所有其他线程。(创建,停止,监视)

 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. 在我的代码中,有一种情况我需要停止线程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. 在我的Wired类中,我有这种方法来更改volatile标志。

        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? 我的问题是..如何从我的MainThread访问方法stopRunning我想要停止的特定线程? Thread.interrupt() doesnt work for me as a solution. Thread.interrupt()对我来说不起作为解决方案。

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. 您应该使keepRunning成为实例变量(属性)而不是静态。

Whenever you want stop a Thread, grab it from Map and set the attribute keepRunning to false using setKeepRunning(false). 每当你想要停止一个线程时,从Map中获取它并使用setKeepRunning(false)将属性keepRunning设置为false。

Don't reinvent the wheel here. 不要在这里重新发明轮子。 Use Thread.interrupt(), and properly check the Thread.isInterrupted() flag, and/or properly handle InterruptedException. 使用Thread.interrupt(),并正确检查Thread.isInterrupted()标志,和/或正确处理InterruptedException。 IE don't swallow it or printStackTrace() it and continue. IE不会吞下它或printStackTrace()它并继续。 If you receive an InterruptedException catch it at the borders of the Runanble.run() method and stop your outer loop and shutdown the thread. 如果您收到InterruptedException,请在Runanble.run()方法的边框处捕获它并停止外部循环并关闭该线程。

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. 如果线程没有卡在某个IO中,那么正确关闭线程非常简单。 If you have a long running task that you don't want to wait for check Thread.isInterrupted() within your logic periodically. 如果你有一个长时间运行的任务,你不想等待在你的逻辑中定期检查Thread.isInterrupted()。 There are no advantages provided by your volatile boolean flag mechanism you have shown over using Thread.interrupted(). 您使用Thread.interrupted()显示的volatile布尔标志机制没有提供任何优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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