Results 1 to 20 of 32
Thread: Stop Thread
- 09-30-2011, 02:11 AM #1
Stop Thread
I'm trying to set up my thread so it can start/interrupted/stop/start/interrupted.... How do I end a Thread? This is what i have so far but it keeps giving errors.
Error:Java Code:public void run() { while (!interrupted()) { try { System.out.println("asf"); Thread.sleep(1500); } catch (InterruptedException ex) { interrupt(); } } stop(); }Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
- 09-30-2011, 02:13 AM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Stop Thread
Try:
instead of just stop();Java Code:Thread.stop();
Regards, James
- 09-30-2011, 02:16 AM #3
Re: Stop Thread
Ok replaced it, but I can't compile. Error with Thread.stop():
non-static method stop() cannot be referenced from a static context
- 09-30-2011, 02:18 AM #4
Re: Stop Thread
What method throws the IllegalThreadStateException exception?
What does the API doc for that method say about why it throws that exception?
- 09-30-2011, 02:19 AM #5
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Stop Thread
Hmmm. what other code do you have?
Maybe post that, like the main() method and stuff relating to your thread.
- 09-30-2011, 02:23 AM #6
Re: Stop Thread
This all the code being used except my GUI generated code.
Java Code:class mainBot extends Thread { public void run() { while (!interrupted()) { try { System.out.println("asf"); Thread.sleep(1500); } catch (InterruptedException ex) { interrupt(); } } Thread.stop(); } }
- 09-30-2011, 02:25 AM #7
Re: Stop Thread
What statement throws the Exception?
- 09-30-2011, 02:26 AM #8
Re: Stop Thread
When I use the sleep() method
Last edited by TyCox94; 09-30-2011 at 02:30 AM.
- 09-30-2011, 02:30 AM #9
Re: Stop Thread
Your first post said you get
Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
Where is this exception thrown?
What is the source statement in your program that was executing when the above error happened?
- 09-30-2011, 02:50 AM #10
Re: Stop Thread
Honestly I haven't got a clue when it's thrown. I don't think it was...?
Ok so i have a start button:Java Code:class mainBot extends Thread { public void run() { while (!interrupted()) { try { System.out.println("asf"); Thread.sleep(1500); } catch (InterruptedException ex) { interrupt(); } } stop(); } }
and also a stop button:Java Code:mainBot.start();
So I run it. Then I click the start button.... Everything works great! So then i click the stop button. The thread stops. Works as needed. Now I try to start(button) the thread again. Doesn't run... error:Java Code:mainBot.interrupt();
Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
-
Re: Stop Thread
- 09-30-2011, 02:53 AM #12
Re: Stop Thread
This is the full Exception
Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:638)
at mainFrame.jButton1ActionPerformed(mainFrame.java:8 4)
at mainFrame.access$000(mainFrame.java:1)
at mainFrame$1.actionPerformed(mainFrame.java:28)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6289)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:605 4)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4652)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 82)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:44 82)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:644)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:617)
at java.awt.EventQueue$2.run(EventQueue.java:615)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
- 09-30-2011, 02:54 AM #13
Re: Stop Thread
Read the API doc for the Thread class. Do a Find for :IllegalThreadStateException
See what methods that error is thrown for and read the doc for the reasons why the exception is thrown.
EDIT: Just saw your error message posted.Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:638)
at mainFrame.jButton1ActionPerformed(mainFrame.java:84)
Read the API doc for the Thread class's start method. That is the method causing the exception.
- 09-30-2011, 03:06 AM #14
Re: Stop Thread
-ops-
Cluster of posts. Sorry about this.Last edited by TyCox94; 09-30-2011 at 03:12 AM.
- 09-30-2011, 03:08 AM #15
Re: Stop Thread
What state is the thread in when the thread's execution exits the run() method?
- 09-30-2011, 03:12 AM #16
Re: Stop Thread
What happened to the doc for some Thread class you posted earlier?
It didn't look like it was from the doc for the start method.
- 09-30-2011, 03:12 AM #17
Re: Stop Thread
Ok quick question. How do i find the state of my thread?
One secondJava Code:System.out.println(Thread.State.values().toString());
Java Code:class mainBot extends Thread { public void run() { while (!interrupted()) { System.out.println("asf"); } System.out.println(Thread.State.values().toString()); }Last edited by TyCox94; 09-30-2011 at 03:14 AM.
- 09-30-2011, 03:14 AM #18
Re: Stop Thread
First you need to see what statement caused the error.
Then you read the API doc for the method that caused the exception and see why it throws the exception.
Then you change your code so it doesn't create the condition for the error.
What does the doc say?
- 09-30-2011, 03:15 AM #19
Re: Stop Thread
Java Platform SE 6
Thrown to indicate that a thread is not in an appropriate state for the requested operation. See, for example, the suspend and resume methods in class Thread.
- 09-30-2011, 03:15 AM #20
Similar Threads
-
Thread help - how to stop?
By scheffetz in forum New To JavaReplies: 3Last Post: 04-10-2011, 01:14 PM -
How to stop Thread
By ersachinjain in forum Threads and SynchronizationReplies: 2Last Post: 11-30-2009, 07:11 PM -
Thread won't stop
By bubbless in forum Threads and SynchronizationReplies: 15Last Post: 10-20-2009, 10:58 PM -
Help to stop a thread
By raghu_lzybns in forum New To JavaReplies: 4Last Post: 07-09-2009, 04:39 PM -
how to stop a thread
By willemjav in forum Advanced JavaReplies: 19Last Post: 09-10-2008, 07:11 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks