Send a message to your background thread telling it to end.

Your background thread:
class MyBackgroundThread extends Thread {
...
...
  private boolean _isRunning = true;
  public void run() {
    ...
    ...
    while (running()) {
      ... // fetch and handle messages. ...
    }
  }

  ..
  private synchronized running() {
    return _isRunning;
  }

  public synchronized stopThread() {
    _isRunning = false;
  }
  ...
}

On May 27, 8:29 am, [email protected] wrote:
> Can I use the thread.join() mechanism?
> Basically, in the onDestroy() method of the service, I would send a
> message to the background thread and call join on the thread.
> The background thread would destroy it self after proper cleanup. But,
> what method should I use to exit the thread.
> Please note the background thread uses a looper to handle messages
> from the main thread.
>
> Or, pl. suggest some other method of implementing the above behavior.
>
> On May 27, 11:31 am, [email protected] wrote:
>
>
>
> > Android may kill the process and thus the service running in the
> > process, onceonDestroy() returns (after sending a message to the
> > worker thread).
> > But how will I make sure that the service has completed it's clean up
> > operations (ex. saving some data on to some storage, etc...), if any?
>
> > The main thread nevers gets blocked in any other scenario exceptonDestroy().
> > Do you have any other mechanism to achieve this behavior in the
> > platform?
>
> > On May 27, 2:23 am, Dianne Hackborn <[email protected]> wrote:
>
> > > Blocking inonDestroy() waiting for the background thread to complete
> > > basically defeats the purpose of doing work in a background thread.
>
> > > On Tue, May 26, 2009 at 7:09 AM, Gautam 
> > > <[email protected]>wrote:
>
> > > > Hi,
>
> > > >     I'm implementing a service that contains a thread to handle all
> > > > time consuming operation. My core service logic is in a different
> > > > thread than the ui or main thread. According to the Android document,
> > > > when the OS plans to free some system resource, it will callonDestroy
> > > > () on the service and only whenonDestroy() returns it will kill the
> > > > process hosting the service, thus giving opportunity to the service to
> > > > cleanup.
>
> > > >     Now, whenonDestroy() is called, I want to send a message to my
> > > > service thread to do the necessary cleanup. Only when the service
> > > > thread acknowledges that the cleanup or shutdown is complete,onDestroy
> > > > () should return.
> > > > I could find a way to send asynchronous messages to threads and the
> > > > corresponding processing of the messages, but not able to figure out
> > > > how I need to implementonDestroy(), such that it would send a message
> > > > to the service thread and should wait for a result, before returning.
>
> > > >    Appreciate if someone can comment on my understanding ofonDestroy
> > > > () and provide some solution to the above problem.
>
> > > > Thanks and Regards,
> > > > Gautam.
>
> > > --
> > > Dianne Hackborn
> > > Android framework engineer
> > > [email protected]
>
> > > Note: please don't send private questions to me, as I don't have time to
> > > provide private support, and so won't reply to such e-mails.  All such
> > > questions should be posted on public forums, where I and others can see 
> > > and
> > > answer them.- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to