Results 1 to 4 of 4
  1. #1
    Chetans is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default Problem in Multi threading.

    How to kill the threads?...
    and how to restart them after some time?
    Can u help me regarding this....

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

    Default

    1. You can't (without using inherently unsafe/dangerous deprecated methods).
    2. You can't. Recreate it.

    If what you really need is a thread that processes something then "goes to sleep" until the next item to process comes along, use a queue.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    To kill a thread, you need to provide some kind of method that causes an exit of the run loop.

    Java Code:
    class KillableThread extends Thread {
    
      boolean keepGoing = true;
      boolean exit = false;
    
      public synchronized void run() {
         while(keepGoing) {
           ....do something useful and then ...
    
            try {
              wait(...maybe have a timeout...);
            } catch (InterruptedException iex) {...handle this} 
         }
         exit = true;
         notify(); 
      }
    
      public synchronized void kill() {
         if(keepGoing) {
           keepGoing = false;
           notify();
    
           if(!exit) {
             try {
               wait();
            } catch (InterruptedException iex) {...do somethign}
           }
         }
      }
    }

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I suggest reading a good tutorial on threads. They are not something to use without having a sound understanding of the basics.

Similar Threads

  1. Newbie to multi-threading please direct me :)
    By kminev in forum Threads and Synchronization
    Replies: 8
    Last Post: 11-18-2009, 06:11 PM
  2. Multi-Threading and process control in Java
    By mo_mughrabi in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2009, 01:47 PM
  3. Problem in threading
    By saurabh in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-01-2008, 08:16 PM
  4. Replies: 0
    Last Post: 07-22-2008, 04:39 PM
  5. question about Multi threading in Java
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:55 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
  •