Results 1 to 14 of 14

Thread: Thread killing

  1. #1
    denis is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default Thread killing

    hello everyone
    So how do i kill a thread in java if it has been started and wont be needed any furthur.
    just curious,
    any help will be greatly appreciated
    thank you
    Denis

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually you can use Thread.stop(), but it's deprecated because it may can cause for thread deadlocks.

    So in Java, it's not possible to kill a thread directly. You may use some meaningful implementation for that. If you need more help on this, let me know here.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    public class Bogus {
      public static void main (String[] args) {
        Thread t = new Thread(
          new Runnable() {
            public void run() {
              try {
                Thread.sleep(5000);  // 5 seconds
              } catch (InterruptedException ie) {
                System.out.println("Interrupted, exiting");
                // perform whatever cleanup is needed
              }
            }
          });
    
        t.start();
        try { Thread.sleep(1000); } catch (InterruptedException ie) {}
        t.interrupt();
      }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Seeing as how the run method will finish as soon as the catch block is done, yes it is. That's the trick. You need to interrupt the thread and let it clean itself up and exit normally.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

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

    Default

    If the thread isn't calling a method that is interruptable when the interrupt is sent, then catch block will never be called.The thread will have to test to see if it has been interrupted and then handle that in normal code, not in a catch block.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Sleep is itself interruptable. Try it out and tell me how long the program sleeps and what it outputs.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Nevermind. I read your post wrong.

    It was, obviously, only meant to be a simple example.
    Last edited by masijade; 09-25-2008 at 07:24 PM.

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    An example of the other way
    Java Code:
    public class Bogus2 {
      public static void main (String[] args) {
        Thread t = new Thread(
          new Runnable() {
            public void run() {
              int i = 0;
              while (!Thread.interrupted())  {
                  System.out.println(i++);
              }
            }
          });
    
        t.start();
        try { Thread.sleep(50); } catch (InterruptedException ie) {}
        t.interrupt();
      }
    }
    And, BTW, there's no reason why the methods can't be combined (directed at the OP, not you Norm).
    Last edited by masijade; 09-25-2008 at 07:51 PM. Reason: Made it simpler

  11. #11
    denis is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default

    public class Bogus2 {
    public static void main (String[] args) {
    Thread t = new Thread(
    new Runnable() {
    public void run() {
    int i = 0;
    while (!Thread.interrupted()) {
    System.out.println(i++);
    }
    }
    });

    t.start();
    try { Thread.sleep(50); } catch (InterruptedException ie) {}
    t.interrupt();
    }
    }
    isnt the above code just for one thread running.

    hello i am a student and a beginner learning MT so my apologies in advance if my questions are not clear.
    so what if we have array of threads and there are concurrent threads running and after some times of processing later we dont need one of the thread, is there a way to kill it.
    since i am doing a client/server app is it that the thread dies if the client disconnects.

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    In that example two threads are running. The original thread, and another one that it started. There is no reason why you can't start more than one additional thread. You need to call the interuppt on the right thread, however. And, no, aside from interrupt (or closing whatever streams it might be reading from) there is no safe way to kill a thread (and for anyone who wants to bring up setting a flag/variable and checking that variable from time to time, this is exactly what interrupt and interrupted do, there is no reason to "roll your own" when it already exists). The stop method exists, but it is deprecated and strongly discouraged.

    Also, if your using a Thread per client, and the client disconnects, your design should be smart enough for the thread servicing that client to recognise that (eventually) and then finish it's work and simply end.
    Last edited by masijade; 09-25-2008 at 09:12 PM.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Norm View Post
    If the thread isn't calling a method that is interruptable when the interrupt is sent, then catch block will never be called.The thread will have to test to see if it has been interrupted and then handle that in normal code, not in a catch block.
    One more point, the test must be in normal code, but the cleanup can still be in a catch block. Simply throw an InterruptedException when interrupted() returns true.

  14. #14
    denis is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. how high-priority thread allow other thread
    By rameshkr in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-15-2009, 11:46 PM
  2. passing a value from parent thread to child thread
    By sachinj13 in forum Threads and Synchronization
    Replies: 7
    Last Post: 09-07-2008, 10:06 PM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 11:30 PM
  4. Replies: 0
    Last Post: 01-28-2008, 08: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
  •