我想在另一个类中使用停止按钮停止多个线程。我尝试在停止线程时使用布尔函数但它不起作用。当用户单击停止按钮时,线程仍在运行,我该如何解决这个问题?
这是主要的课程
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{
...
}
答案 0 :(得分:1)
您有两个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);
您还应该确保在其他线程中可以看到stop方法中paused
的更新。您应该使用停止方法synchronized
(或使用AtomicBoolean
,或在线程之间使用正确的信号方式,如互斥锁或信号量。)
答案 1 :(得分:0)
我看到多种原因导致它不起作用。
ElevatorSystem.mt
是Main_Thread
的另一个实例,是ElevatorSystem.MAIN_THREAD
使用的实例。MAIN_THREAD
在任何地方都已启动。Main_Thread
希望其标记pauseThreadFlag
为false
停止,但Main_Thread.stopThread
将其设置为true
。Main_Thread.checkForPaused
致电wait
,但我没有看到它被唤醒的地方。ElevatorSystem.MAIN_THREAD
也应该阻止其他线程的原因。