I have code like below. I have the following problem: I must stop Thread t
, but inside is a method which suspends the thread in one place for long time. So when I'm trying to stop it, it stops after that method is executed. Anyone know how to terminate this thread or stop it in any other way while executing the method inside the thread?
public class MyClass extends JFrame implements Runnable{
@Override
public void run() {
OtherClass oc = new OtherClass();
Runnable r = oc;
Thread t = new Thread(r);
t.start();
addButton(buttonPanel, "Stop it", new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println("stopping");
t.stop();
setVisible(false);
}
});
}
}
public void addButton(Container c, String title, ActionListener listener) {
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}