代码之家  ›  专栏  ›  技术社区  ›  Temmie

线程不会停止使用按钮(多线程)

  •  3
  • Temmie  · 技术社区  · 7 年前

    我想使用另一个类中的停止按钮来停止多个线程。我尝试使用布尔函数停止线程,但它无法工作。当用户单击停止按钮时,线程仍在运行,我该如何解决这个问题?


    这是主课

    public class ElevatorSystem extends javax.swing.JFrame {
        public static int currentFloor = 1;
    
        Thread ElevatorMove = null;
        Thread PeopleMove = null;
        static Thread RandomPeople = null;
        Thread MAIN_THREAD = new Thread(new Main_Thread());
        private Main_Thread mt = new Main_Thread();
        ...
    
        public ElevatorSystem() {
            initComponents();
            randWeight();
            this.setResizable(false);
            if(pplw.isEmpty())
                elev_weight = 0;
            else{
                for(int s:pplw)
                    elev_weight += s;
    
            }
            RandomPeople = new Thread(new Randomize_People(EntryFloor1,EntryFloor2,EntryFloor3,EntryFloor4,EntryFloor5));
            RandomPeople.setPriority(Thread.MAX_PRIORITY-1);
        //Start Adding peeoples
            RandomPeople.start();
        }
    
        ...
        private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {                                        
            //not working...
            if(MAIN_THREAD.isAlive()){
                try {
                    mt.stopThread();
                } catch (InterruptedException ex) {
                    Logger.getLogger(ElevatorSystem.class.getName()).log(Level.SEVERE, null, ex);
                }
            }else{
                this.setEnabled(false);
                JOptionPane.showMessageDialog(this,"Thread Not Running!","Thread Not Running!",JOptionPane.INFORMATION_MESSAGE);
                this.setEnabled(true);
            }
        }   
    }
    

    这是线程

    class Main_Thread implements Runnable{
        ....
        private final Object ElevatorSystem = new Object();
        private boolean pauseThreadFlag = false;
    
        Main_Thread(){
    
        }
    
        Main_Thread(
            JTextField EntryFloor1, JTextField ExitFloor1,
            JTextField EntryFloor2, JTextField ExitFloor2,
            JTextField EntryFloor3, JTextField ExitFloor3,
            JTextField EntryFloor4, JTextField ExitFloor4,
            JTextField EntryFloor5, JTextField ExitFloor5,
            JTextField Ceiling,
    
            JTextArea Elev1,   JPanel Elev1Container,
    
            String Elevator_Status, int currentFloor,int elev_weight, ArrayList pplw
        ){
            this.EntryFloor1 = EntryFloor1; this.ExitFloor1 = ExitFloor1;
            this.EntryFloor2 = EntryFloor2; this.ExitFloor2 = ExitFloor2;
            this.EntryFloor3 = EntryFloor3; this.ExitFloor3 = ExitFloor3;
            this.EntryFloor4 = EntryFloor4; this.ExitFloor4 = ExitFloor4;
            this.EntryFloor5 = EntryFloor5; this.ExitFloor5 = ExitFloor5;
    
            this.Elev1 = Elev1;
            this.Elev1Container = Elev1Container;
    
            this.Elevator_Status = Elevator_Status;
            this.currentFloor = currentFloor;
    
            this.elev_weight = elev_weight;
    
            this.pplw.addAll(pplw);
        }
        @Override
        public void run() {
            System.out.println("Running...");
            System.out.println("Inside Weight: "+pplw);
            while(true){
                checkForPaused();
                //whole process
           }
        }
    
        private void checkForPaused() {
           synchronized (ElevatorSystem) {
               while (pauseThreadFlag) {
                   try {
                       ElevatorSystem.wait();
                   } catch (Exception e) {}
               }
           }
        }
    
        public void stopThread() throws InterruptedException {
            pauseThreadFlag = true;
        }
    }
    
    class People_Move implements Runnable{
        ...
    }
    
    class Elevator_move_UP implements Runnable{
        ...
    }
    
    class Elevator_move_DOWN implements Runnable{
        ...
    }
    
    class Randomize_People implements Runnable{
        ...
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Andy Turner    7 年前

    您有两个实例 Main_Thread :

    Thread MAIN_THREAD = new Thread(new Main_Thread());
    private Main_Thread mt = new Main_Thread();
    

    你在阻止一个,但在观察另一个。

    if(MAIN_THREAD.isAlive()){
      try {
        mt.stopThread();
    

    只需使用单个实例:

    private Main_Thread mt = new Main_Thread();
    Thread MAIN_THREAD = new Thread(mt);
    

    您还应该确保更新 paused stop方法在其他线程中可见。你应该使用stop方法 synchronized (或使用 AtomicBoolean ,或者在线程之间使用适当的信令方式,如互斥或信号量)。

        2
  •  0
  •   mm759    7 年前

    我看到了它不起作用的多种原因。

    1. ElevatorSystem.mt 是的另一个实例 Main_Thread 作为使用的 ElevatorSystem.MAIN_THREAD .
    2. 我不明白 MAIN_THREAD 从任何地方开始。
    3. 班级 主_线程 希望是国旗 pauseThreadFlag 成为 false 停下来,但是 Main_Thread.stopThread 将其设置为 true .
    4. Main_Thread.checkForPaused 电话 wait ,但我看不到它被唤醒的地方。
    5. 我看不出为什么要停下来 电梯系统.MAIN_THREAD 也应该停止其他线程。