我有一個線程正在運行,但從外部我無法繞過一個值來停止該線程。如何在Mytest()
內發送錯誤/真值或調用正在運行的線程公共方法?當我按下按鈕1? 例如:thread.interrupt();
runnable.stop();
或runnable.start();
如何訪問正在運行的線程/ runnable?
// 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.stop()被調用時,它僅回採只在循環?但該實例仍未關閉?在這種代碼模式中?然後Thread.stop()或Thread.interrupt()需要殺死池? – YumYumYum 2011-04-06 07:51:49
當'run'方法結束時,線程停止。如果需要,您可以將Mytest實例附加到新的線程,或者在現有線程上調用'start()',以便再次運行。此代碼只是確保運行代碼將在合理的時間範圍內完成(如果標誌不是不穩定的,則不是這種情況) – 2011-04-06 07:55:02
Thanks!請參閱上面的最新更新。所以你的意思是,如果我們標記while循環來停止標誌,它會自動破壞線程,如果我們將它標記爲start(),它會重新創建線程? – YumYumYum 2011-04-06 08:15:19