mdenis
August 2nd, 1999, 04:27 AM
What is the proper way to stop the thread ? Thread.stop() is deprecated and furthermore doesn't seem to work corectly. Help please !
|
Click to See Complete Forum and Search --> : Stopping thread mdenis August 2nd, 1999, 04:27 AM What is the proper way to stop the thread ? Thread.stop() is deprecated and furthermore doesn't seem to work corectly. Help please ! unicman August 2nd, 1999, 07:54 AM What is the safe way to stop a thread depends on the logic inside the thread. There is a detailed article in JDK1.2 help. It is quite explanative and a bit confusing also (if u r not going to develop a "heavy" thread). If u want to create a simple thread which should stop safely when u say 'stop', it is recommended that u don't use 'Thread.stop()' method. So what should u do? 1. In most cases the thread is doing some repeatative things in its 'run' method. Obviously the 'run' method will have a "loop". Here it is very simple to implement 'stop' functionality as shown. public class TestClass extends Thread { private mbRUN = true; public TestClass() { // Do ur normal coding (initialization etc) here. } public void stop() { mbRUN = false; // The difference here is, u r "requesting" the thread to stop so that it will "stop" // when it is in a safe state } public void run() { mbRUN = true; // Use this statement if u want to run the thread after // it was stopped previously. // There may be any loop, in place of 'while' ... while( (...your conditions...) && mbRUN ) { // here u will have the repeatative things, which u want to run in // this thread. } // here all ur cleaning code will be there } } As shown in the above example, u don't have to make extensive modifications to ur logic (only code added is for setting and checking 'mbRUN' value). This way u know that whenever 'stop' method of above class is called, it doesn't stop immediately. It will complete its iteration, and when it will check 'mbRUN', it will know that it should stop and come out safely. I don't know if u r convinced with this answer, but if u want more details tell me. I won't say I know everything, but I will surely help as far as I know. - UnicMan http://members.tripod.com/unicman Septimiu August 2nd, 1999, 08:05 AM the method thread.stop() isn't deprecated. u may use if(threat != NULL)<br unicman August 2nd, 1999, 08:12 AM I think u r referring to JDK1.1. Its not deprecated in JDK1.1, but it is depricated in JDK1.2. Please confirm. - UnicMan http://members.tripod.com/unicman mdenis August 2nd, 1999, 08:15 AM Thanx for your response. But the way you suggest cannot really help me. In you example the "loop" doesn't break immediately but first comletes its current iteration. In my program such an iteration may take a lot of time (it waits for a response from remote host). And I wanted it to stop immediately. Can you help me ? Septimiu August 2nd, 1999, 08:30 AM I worked only with Visual J++ 1.1 unicman August 2nd, 1999, 01:04 PM Well if your iteration does many things, what u can do is keep a simple condition in your loop... while ( ... ) // the same loop I had written { .... // do ur stuff if( !mbRUN ) break; .... // do ur stuff if( !mbRUN ) break; .... // do ur stuff } I know this is tedious, but it atleast ensures that ur thread and objects r in safe state. And talking about socket, u can keep 'Socket' as member variable in your thread. And then in 'stop' method close the connection. When u close connection of a 'Socket' it will break off and u can then come out of 'run' method safely. - UnicMan http://members.tripod.com/unicman codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |