繁体   English   中英

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

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

所以我有我的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();
      }

在我的代码中,有一种情况我需要停止线程t1。

我的有线课程:

   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();}  

        }
        }

在我的Wired类中,我有这种方法来更改volatile标志。

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

我的问题是..如何从我的MainThread访问方法stopRunning我想要停止的特定线程? Thread.interrupt()对我来说不起作为解决方案。

我看过很多关于这个问题的类似问题,但我没有找到适合我案例的东西。 对不起,如果我错过了这个代码是我的实际代码的过度简化

您应该使keepRunning成为实例变量(属性)而不是静态。

每当你想要停止一个线程时,从Map中获取它并使用setKeepRunning(false)将属性keepRunning设置为false。

不要在这里重新发明轮子。 使用Thread.interrupt(),并正确检查Thread.isInterrupted()标志,和/或正确处理InterruptedException。 IE不会吞下它或printStackTrace()它并继续。 如果您收到InterruptedException,请在Runanble.run()方法的边框处捕获它并停止外部循环并关闭该线程。

您的方法应更改为:

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

如果线程没有卡在某个IO中,那么正确关闭线程非常简单。 如果你有一个长时间运行的任务,你不想等待在你的逻辑中定期检查Thread.isInterrupted()。 您使用Thread.interrupted()显示的volatile布尔标志机制没有提供任何优势。

暂无
暂无

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

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