我正在用eclipse中窗口生成器的幫助創建一個GUI,創建一個簡單的java程序。該GUI由一個按鈕組成。Java線程啓動 - 停止啓動在Eclipse中點擊相同的按鈕
我的目標: - 點擊按鈕後,啓動一個線程,無限次地打印到控制檯號碼,直到再次單擊相同的按鈕來停止。
這裏是我的實現: -
Runner.java(Thread類)
public class Runner extends Thread{
private volatile boolean running = true;
private int i = 1;
@Override
public void run() {
while(running)
{
System.out.println(i++ +"\n");
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void shutdown()
{
running = false;
}
}
MainGUI.java(ui類)
Runner runIT = new Runner();
final JButton btnNewButton = new JButton("Start Thread");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String buttonText = btnNewButton.getText();
if(buttonText.equals("Start Thread"))
{
btnNewButton.setText("Stop Thread");
runIT.start();
}
else if(buttonText.equals("Stop Thread"))
{
btnNewButton.setText("Start Thread");
runIT.shutdown();
}
}
});
我的問題: - 線程開始和停止完美,但只是一次。也就是說,我只能啓動和停止線程一次。當我嘗試重新啓動停止的線程時,它會失敗。 我希望線程繼續從停止的相同值打印數字。
Error :- Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:705)
at com.zakoi.java.thread.GUI.MainGUI$2.actionPerformed(MainGUI.java:62)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3311)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
請幫忙。
您不能再次啓動停止的線程。在「開始」上創建一個新實例。閱讀JavaDoc瞭解更多詳情:http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start%28%29 – Tom 2014-09-24 07:54:08