Results 1 to 4 of 4
  1. #1
    userj2ee is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default Stopping a thread

    Hi,

    Can anyone tell me what are the different ways to stop a thread?

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by userj2ee View Post
    Hi,

    Can anyone tell me what are the different ways to stop a thread?

    Thanks
    The only safe way is to programmatically ask it to stop; of course the thread has to cooperate, i.e. there is no safe way to slice its throat and kill it. Your runnable method has to test, say, a boolean variable repeatedly and when it has been set to true (by another thread) it has to quit.

    kind regards,

    Jos

  3. #3
    userj2ee is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default Stopping a thread

    thanks for your reply Jos.

    As u suggested that we should use the approach of having a boolean variable to stop a thread. The same approach is discussed at multiple places where use of volatile variable is recommended so that all threads should refer to the same copy. I understood this concept. But have one confusion .. Why volatile has to be used? Cant we achieve the same by using a static variable instead of volatile?

    Thanks

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by userj2ee View Post
    thanks for your reply Jos.

    As u suggested that we should use the approach of having a boolean variable to stop a thread. The same approach is discussed at multiple places where use of volatile variable is recommended so that all threads should refer to the same copy. I understood this concept. But have one confusion .. Why volatile has to be used? Cant we achieve the same by using a static variable instead of volatile?

    Thanks
    If you want to stop everything at once a static condition variable would do; and yes, always make those variables volatile because a compiler might, and pay special attention to the verb 'might', optimize out the test for non volatile variables; basically your Runnable should look like this:

    Java Code:
    public void run() {
       while (!stop) {
          process(); // do real work here ...
       }
    }
    somewhere else in that class you have a (static) volatile boolean stop variable that can be set to true by another thread.

    kind regards,

    Jos

Similar Threads

  1. Stopping a .swf with java
    By ercarls in forum New To Java
    Replies: 2
    Last Post: 04-14-2010, 06:33 PM
  2. stopping second time round
    By silverspoon34 in forum New To Java
    Replies: 0
    Last Post: 11-20-2009, 02:04 PM
  3. Pong Paddle Not Stopping Ball At Certain Speed
    By JDCAce in forum Java Applets
    Replies: 3
    Last Post: 04-01-2009, 11:07 PM
  4. Replies: 11
    Last Post: 01-26-2009, 12:22 AM
  5. stopping thread...using flags
    By rstepler in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 09:36 PM

Posting Permissions

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