Page 1 of 2 12 LastLast
Results 1 to 20 of 32
Like Tree1Likes

Thread: Stop Thread

  1. #1
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default 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.

    Java Code:
        public void run() {
            
            while (!interrupted()) {
                try {
                    System.out.println("asf");
                    Thread.sleep(1500);
                } catch (InterruptedException ex) {
                    interrupt();
                }
            }
            stop();
        }
    Error:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Stop Thread

    Try:
    Java Code:
    Thread.stop();
    instead of just stop();

    Regards, James

  3. #3
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default 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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Stop Thread

    What method throws the IllegalThreadStateException exception?
    What does the API doc for that method say about why it throws that exception?

  5. #5
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Stop Thread

    Hmmm. what other code do you have?
    Maybe post that, like the main() method and stuff relating to your thread.

  6. #6
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default 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();
        }
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Stop Thread

    What statement throws the Exception?

  8. #8
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Stop Thread

    When I use the sleep() method
    Last edited by TyCox94; 09-30-2011 at 02:30 AM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default 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?

  10. #10
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Stop Thread

    Honestly I haven't got a clue when it's thrown. I don't think it was...?

    Java Code:
    class mainBot extends Thread {
        
        public void run() {
            
            while (!interrupted()) {
                try {
                    System.out.println("asf");
                    Thread.sleep(1500);
                } catch (InterruptedException ex) {
                    interrupt();
                }
            }
            stop();
        }
    }
    Ok so i have a start button:

    Java Code:
    mainBot.start();
    and also a stop button:

    Java Code:
    mainBot.interrupt();
    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:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Stop Thread

    Quote Originally Posted by TyCox94 View Post
    Honestly I haven't got a clue when it's thrown. I don't think it was...?
    The exception tells you exactly where it's thrown. Please post the entire text of the exception message.

  12. #12
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default 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)

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default 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.

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:638)
    at mainFrame.jButton1ActionPerformed(mainFrame.java:84)
    EDIT: Just saw your error message posted.
    Read the API doc for the Thread class's start method. That is the method causing the exception.

  14. #14
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Stop Thread

    -ops-

    Cluster of posts. Sorry about this.
    Last edited by TyCox94; 09-30-2011 at 03:12 AM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Stop Thread

    What state is the thread in when the thread's execution exits the run() method?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default 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.

  17. #17
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Stop Thread

    Ok quick question. How do i find the state of my thread?

    Java Code:
    System.out.println(Thread.State.values().toString());
    One second

    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.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default 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?

  19. #19
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default 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.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

Page 1 of 2 12 LastLast

Similar Threads

  1. Thread help - how to stop?
    By scheffetz in forum New To Java
    Replies: 3
    Last Post: 04-10-2011, 01:14 PM
  2. How to stop Thread
    By ersachinjain in forum Threads and Synchronization
    Replies: 2
    Last Post: 11-30-2009, 07:11 PM
  3. Thread won't stop
    By bubbless in forum Threads and Synchronization
    Replies: 15
    Last Post: 10-20-2009, 10:58 PM
  4. Help to stop a thread
    By raghu_lzybns in forum New To Java
    Replies: 4
    Last Post: 07-09-2009, 04:39 PM
  5. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •