How to Kill a Thread
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   eric_chang
Posted On:   Friday, October 29, 2004 03:17 AM

Dear Sir, I understand I need to use interrupt to kill the Thread but I can't apply in my program. The program is like this : public void run() { // There are a thousand lines of codes in the following without looping System.out.println("This is statement l"); System.out.println("This is statement 2"); System.out.println("This is statement 3"); ... ... } If interrupt() need to be used, the while loop must be used to check the value as below; public void run() { while (!interrupted()) { System.out.println("This is statement l"); Syst   More>>

Dear Sir,



I understand I need to use interrupt to kill the Thread but I can't apply in my program.




The program is like this :




public void run() {

// There are a thousand lines of codes in the following without looping



System.out.println("This is statement l");

System.out.println("This is statement 2");

System.out.println("This is statement 3");

...

...

}

If interrupt() need to be used, the while loop must be used to check the value as below;




public void run() {

while (!interrupted()) {

System.out.println("This is statement l");

System.out.println("This is statement 2");

System.out.println("This is statement 3");

...

...

}

}

Finally, the program logic is changed and the result is different. I would like to stop the thread at any time.





Furthermore there are a thousands of the different report(one thread per one report) exists in our system. How can I overcome the problem if stop method cannot be used.





For the reports, there are no lock mechansim and it is only a read process, if I insist to use the 'stop', is it any other side effect other than the lock or memory
resource




Please help !!!


Thanks & Regards,

Eric    <<Less

Re: How to Kill a Thread

Posted By:   Edward_Harned  
Posted On:   Friday, October 29, 2004 07:33 AM

Read the API documentation on how to stop a thread. Its under the thread.stop() method. Don't use interrupt.



Test a private variable for the stop condition and when true, get out of the loop.



while (!posted) {




// do your work here


// When the variable is set true

if  (posted) {


    // get out of loop

    break;


}



// do more work here




}

About | Sitemap | Contact