简体   繁体   English

无法停止线程循环

[英]Can't stop loop in thread

Can someone explain to me why my thread "m" isn't stopping when the logout jbutton is pushed? 有人可以向我解释为什么注销jbutton时我的线程“ m”没有停止吗? program seems to being hanging or in some infinite loop.I'm having trouble understanding what is going on with the thread in this case. 程序似乎正在挂起或处于无限循环中。在这种情况下,我无法理解线程的状况。

public class Log_GUI extends Jframe{
    private GUI talking;
    private volatile boolean[]go = new boolean[1]; 


                    go[0] = true;
                    setVisible(false);
                    GUI talking = new GUI(read_me,display_me,usr_name);
                    talking.create(go);


                    Thread m = new Thread(){
                      public void run(){

                          String me;
                          while (go[0]){

                              try
                              {


                                  try {
                                      me = (String) read_me.readObject();
                                      System.out.println(me);
                                      talking.sendme(me);

                                  }catch (ClassNotFoundException ClassNotfoundException){
                                  }
                              }catch (IOException IOException){
                              }
                          }
                      }

                    }; m.start();
                }

            } catch (ClassNotFoundException ClassNotFoundException) {

            }

    }catch (IOException IOException){

        }

        System.out.println("hello");
}
}



    public class GUI extends Jframe{

     public void create(boolean[]go){
       .........
         sign_out.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            go[0]=false;

        }
    });
    }
    }

You have a concurrency issue. 您有并发问题。 volatile boolean[]go = new boolean[1]; only solves visibility and atomicity for variable go but it does not provide any guarantees for the values inside the array. 仅解决变量go可见性和原子性,但不为数组内的值提供任何保证。

In a nutshell if thread 2 modifies go[0] to be true , thread 1 might never see it... if thread 2 creates a new array and assigns it to go then thread 1 will see it... 简而言之,如果thread 2go[0]修改为true ,则thread 1可能永远不会看到它...如果thread 2创建了一个新数组并将其分配给go那么thread 1将看到它...

In method actionPerformed() you must do something like actionPerformed()方法中,您必须执行以下操作

boolean[] copy = copy(go);
copy[0] = false;
go = copy; // correct way to do it is CAS unless you know you are the only writer...

Also i dont understand the purpose of keeping array, in your case you could just keep one boolean flag (unless you intend to store multiple flags)... 我也不明白保持数组的目的,在您的情况下,您只能保留一个布尔值标志(除非您打算存储多个标志)...

If so you could do cheap concurrency using bitset approach, when you use single volatile int or long and set single bit in it... that allows you to have atomicity of change and visibility using single volatile for 32 or 64 flags... 如果是这样,您可以使用位集方法进行廉价的并发,当您使用单个volatile int或long并在其中设置单个位时...使用32个或64个标志的单个volatile可让您拥有更改和可见性的原子性...

尝试一起删除局部变量go[] ,始终从talking.stop();选择最新状态talking.stop();

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

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