Yep, that is pretty much the safe of doing this. In this way, although a bit cumbersome, you know how your thread will be terminated and that your app's state is in a predictable state. Calling 'stop' on a thread is deprecated, because it can put your app in an unpredictable state.
Another thing you could do is to get hold of your long-lasting communications with the server and the database. Get the connections. If you want to stop the thread, set that 'stop' variable and forcefully close all open connections. You have to catch IOExceptions in your IO-code, though, to properly handle this. Also, try calling 'interrupt' on the thread. This may cause synchronized 'wait()' statements, i.e. sleeping threads, to wake up and speed up your thread-shutdown. Again, you need to catch and handle InterruptedExceptions to properly handle this. On Aug 28, 6:56 pm, jsdf <[email protected]> wrote: > Hi Android experts, > I have a tricky question that I would like some advice on. > > I have a thread that does a large amount of network and database > work. E.g.: > > new Thread(new Runnable() { > public void run() { > String largeData = getLargeDataFromServer(); > Object largeObject = xmlParser.parseFromXml(largeData); > saveLargeObjectToDatabase(); > putLargeDataToSecondServer(); > } > > }).start(); > > The problem I have is that I need to stop this thread at any point. > It does not need to stop absolutely immediately, but given limited > network and memory on the phone, the sooner I can stop it the better. > > I currently am testing and stopping with a method similar to this: > > new Thread(new Runnable() { > public void run() { > if (shouldStop) return; > String largeData = getLargeDataFromServer(); > if (shouldStop) return; > Object largeObject = xmlParser.parseFromXml(largeData); > if (shouldStop) return; > saveLargeObjectToDatabase(); > if (shouldStop) return; > putLargeDataToSecondServer(); > } > > }).start(); > > This makes me very uncomfortable, and litters my code with these > shouldStop statements. Furthermore, if the system is in any of the > four long functions, that function has to complete before the test can > actually happen. > > I notice that there is a Thread.stop(), but the comments warn against > using it. > > Does anyone have suggestions on how to reconfigure the thread so I can > stop relatively quickly and regardless of whether execution is in one > of its subfunctions? > > Thanks so much in advance, > jsdf --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

