Results 1 to 5 of 5

Thread: Stoping thread?

  1. #1
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Exclamation Stoping thread?

    Please help me, i want to stop a thread when i press a button and its using actionperformed. But when i am implementing it it give me an exception.
    Code is like this
    Java Code:
     MThread m = new MThread();
             new Thread(m).start();
             JButton b = new JButton("Stop");
             b.addActionListener(this);
             public void actionPerformed()
              {
               if(e.getSource() == b)
               new Thread(m).stop();
               }

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

    Default

    1) You know that when you create a new Thread object, it is completely distinct from the previous Thread object, that calling methods on the one will have no effect on the other.

    2) Also, the API will tell you that Thread#stop() has been deprecated, why it has been deprecated and what to do instead.

    I recommend that you create a small compilable and runnable demo program and post it here, an SSCCE (see the link below).

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by greatmajestics View Post
    Please help me, i want to stop a thread when i press a button and its using actionperformed. But when i am implementing it it give me an exception.
    Code is like this
    Java Code:
     MThread m = new MThread();
             new Thread(m).start();
             JButton b = new JButton("Stop");
             b.addActionListener(this);
             public void actionPerformed()
              {
               if(e.getSource() == b)
               new Thread(m).stop();
               }
    Did you read the API documentation what it has to say about that stop() method? Even more you are trying to stop a brand new Thread before it even has started.

    kind regards,

    Jos

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

    Default

    Jos, I beat you in this one, but you got the draw in the other thread. So I guess that makes us even.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    Jos, I beat you in this one, but you got the draw in the other thread. So I guess that makes us even.
    Yep, it's even but it's Sunday so it doesn't count ;-)

    kind regards,

    Jos

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 02:13 AM
  3. Stoping repeated entries in JComboBox
    By MasterDeveloper in forum AWT / Swing
    Replies: 5
    Last Post: 04-05-2009, 12:07 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 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
  •