У меня есть поток, но извне я не могу обойти значение, чтобы остановить этот поток. Как я могу отправить значение false/true внутри Mytest()
или вызвать текущие общедоступные методы потока? Когда я нажимаю кнопку1?
ex: thread.interrupt();
runnable.stop();
или runnable.start();
// Main
public class Main extends JFrame
{
public static Runnable runnable;
public static Thread thread;
private JButton b1 = new JButton("Start/Stop");
public void init()
{
//Execute a job on the event-dispatching thread:
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
public void createGUI()
{
Container cp = getContentPane();
b1.addActionListener(new button1()); cp.add(b1);
runnable = new Mytest();
thread = new Thread(runnable);
thread.start();
}
}
// Button 1 - [problem to go inside a running thread]
public class button1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("button pressed - need to access ");
//thread.interrupt(); runnable.stop(); //or runnable.start();
}
}
// Running - Thread
public class Mytest implements Runnable
{
public static boolean onoff = false;
public static boolean status = false;
public void run()
{
while(true)
{
if (onoff)
{
return;
} else {
if (status==false) System.out.println("running");
}
}
}
public static void stop() { status = true; onoff=true; }
public static void start() { status = false; onoff = false; }
}
Последующее наблюдение (подтверждение):
Step 1:
/* Main - boot/startup */
public class Main extends JFrame
{
public static Mytest runnable; // wrong: public static Runnable runnable;
public static Thread thread;
private JButton b1 = new JButton("Start");
private JButton b2 = new JButton("Stop");
public void init()
{
// Execute a job on the event-dispatching thread:
// In case Freezed for heavy lifting
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
public void createGUI()
{
Container cp = getContentPane();
b1.addActionListener(new button1());
cp.add(b1);
runnable = new Mytest();
thread = new Thread(runnable);
try {
thread.sleep(100); // value is milliseconds
thread.start();
} catch (InterruptedException e) {
}
}
public static void main(String[] args)
{
run(new Main(), 500, 500);
}
public static void run(JFrame frame, int width, int height)
{ ...
frame.setVisible(true);
}
}
/* To start */
public class button1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
runnable.start();
}
}
/* To stop */
public class button2 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
runnable.stop();
}
}
Step 2:
/* Thread deals */
public class Mytest implements Runnable
{
private static volatile boolean running = true;
public void run()
{
while(running)
{
// do stuff
}
}
public void start() { running = true; }
public void stop() { running = false;}
}