我有一个线程正在运行,但从外部我无法绕过一个值来停止该线程。我怎样才能在里面发送假/真值Mytest()
或者调用运行线程的公共方法?当我按下按钮1时?
前任:thread.interrupt();
runnable.stop();
or 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;}
}
如果你通过类而不是作为一个来定义它Runnable
您可以调用实例方法。
public static Mytest runnable;
另请注意,由于多个内核拥有自己的关联内存,因此您需要警告处理器,另一个处理器上的状态可能会发生更改,并且需要监视该更改。听起来很复杂,但只需将“易失性”关键字添加到布尔标志中即可
public class Mytest implements Runnable
{
private static volatile boolean running = true;
public void run()
{
while(running) {
// do stuff
}
}
public void stop() { running = false;}
}
开始Runnable
如您的初始代码所示,然后使用将其关闭runnable.stop()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)