stopping a thread


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: stopping a thread

  1. #1
    Join Date
    Aug 2005
    Posts
    9

    stopping a thread

    ok i've got a problem stopping a thread...

    this thread runs a recursive function... and while it is running the function.... i want to stop it...

    i've tried threadPointer = null; but it doesn't work.. the thread still continues running....

    any suggestions??

    TIA

  2. #2
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Be patient Icube

    Where is the thread?

  3. #3
    Join Date
    Nov 2006
    Posts
    13
    do you mean thread.stop(); which will terminate the execution of a thread.If you going to use the thread again i think you should call thread.suspend(); instead then thread.resume(); to start it again

  4. #4
    Join Date
    Aug 2005
    Posts
    9
    Quote Originally Posted by dojo
    do you mean thread.stop(); which will terminate the execution of a thread.If you going to use the thread again i think you should call thread.suspend(); instead then thread.resume(); to start it again
    i believe the methods u mentioned are deprecated....



    Quote Originally Posted by Kinda Electroni
    Where is the thread?
    the thread executes a recursive function.. this function is working correctly... no problems.... i just want to end it manually....

    here is what i tried... i tried to use an exit flag to return back all the recursive function... which still doesn't work


    class A extends thread{

    boolean exit = false;

    recursive_fn(argument){
    if(exit){return;}
    ....
    recursive_fn(argument);
    }
    }

    main(){
    A a = new A();
    a.start();
    }


    button_pressed(){
    a.exit = true;
    }

  5. #5
    Join Date
    Nov 2006
    Posts
    12
    if you want to end it manually you have the right idea.

    Code:
    public class Recurse extends Thread{
        boolean running = false;
    
      public Recurse(){
             // constructor stuff here
             this.start();
    }
        
       public void run(){
              running = true;
              while(running){
                   //your code here
    }
    
       public void halt(){
              running = false;}
    }
    this will run the loop till the halt method is called causing the flag to be set to false. The stop() method should not be used because it stops the execution immediately and may result in data corruption and general messiness.

  6. #6
    Join Date
    Aug 2005
    Posts
    9
    but i tried the method... it still doesn't work?

  7. #7
    Join Date
    Nov 2006
    Posts
    12
    Which method isn't working? The recursive or the run method? If your trying to get the thread to run a recursive method the output of the method needs to be handled in the run method or you need to pass an argument to the run method with an object from your calling thread to "capture" the output.

    Just to make sure you realize you need to call halt() in order to stop the thread.

  8. #8
    Join Date
    Aug 2005
    Posts
    9
    Quote Originally Posted by Red_Jester
    Which method isn't working? The recursive or the run method? If your trying to get the thread to run a recursive method the output of the method needs to be handled in the run method or you need to pass an argument to the run method with an object from your calling thread to "capture" the output.

    Just to make sure you realize you need to call halt() in order to stop the thread.
    i think u misunderstood me.... i've already started the thread successfully... my problem is i can't STOP the thread while it is running as written in the thread title, the code i wrote above is how i tried to stop it but it din't, the recursive function still continues running.

  9. #9
    Join Date
    Aug 2005
    Posts
    9
    ok finally got it to stop. thanks for all the help

    cheers

Similar Threads

  1. Replies: 4
    Last Post: 04-27-2006, 08:01 AM
  2. Stopping Threads/Runnables
    By Shaitan00 in forum Java
    Replies: 2
    Last Post: 04-14-2005, 02:54 AM
  3. Infragistics/Sheridan new registration policy
    By Lowell Williams in forum vb.announcements
    Replies: 18
    Last Post: 02-10-2001, 06:22 AM
  4. Replies: 1
    Last Post: 10-29-2000, 05:51 PM
  5. Replies: 2
    Last Post: 10-27-2000, 04:07 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center