When does my thread die?

by g1bb » Fri, 23 Jan 2009 22:16:00 GMT


Sponsored Links
 Hello,

I've created an app that functions as a countdown timer via a thread,
a wakelock, and a handler back to my activity. My question is, what
will cause my thread to die, or app to not be running in memory
anymore? I've had this happen once, and can't seem to recreate it
again.

Thanks in advance!
--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by Torgny » Fri, 23 Jan 2009 23:36:39 GMT


 Your application background process might get killed if you run
another application that takes up a lot of memory, for instance the
browser with several windows. As far as I understand it, the
application in the foreground takes priority as far as memory and
processing power goes over services and threads.



--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


When does my thread die?

by g1bb » Fri, 23 Jan 2009 23:47:45 GMT


 I realized this right after I posted, by running a bunch of other apps
while my app was still running. It seems like 'setPersistent' on the
activity should most likely prevent this. Any ideas?

Thanks again.




--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by James Patillo » Sat, 24 Jan 2009 01:36:24 GMT


 Your thread will likely be killed in a bad way in onDestroy(). You should
therefore kill it yourself, for example in either onStop() or onDestroy().

Apps are killed, for the most part and from what I can tell when the back
button is used to exit the app, or the Home button is used and the system
needs the memory. The back button seems to cause the app to call onDestroy()
while the home button seems to cause the app to call onStop() and stay in
memory.

This is only what seems to happen most of the time, and isn't necessarily
what will happen in a given instance.

-----Original Message-----
From: g1bb [mailto:corymgibb...@gmail.com] 
Sent: Friday, January 23, 2009 8:16 AM
To: Android Developers
Subject: [android-developers] When does my thread die?


Hello,

I've created an app that functions as a countdown timer via a thread,
a wakelock, and a handler back to my activity. My question is, what
will cause my thread to die, or app to not be running in memory
anymore? I've had this happen once, and can't seem to recreate it
again.

Thanks in advance!


--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by Dianne Hackborn » Sat, 24 Jan 2009 01:45:24 GMT


 




DO NOT DO THAT.

Please read this:  http://code.google.com/android/intro/lifecycle.html 

Basically you should have things you want to continue running in the
background implemented as a Service.

Also there is generally no need to create a whole new Thread for this kind
of stuff, you can just post delayed messages to your own handler.

Finally, for something like a countdown timer, you really might want to
consider using the alarm manager so you don't need to keep your app running
at all while it is in the background.  That is the kind of thing a well
behaving Android app will do.  To be able to show the remaining time if the
user returns to your activity, you can store on SharedPreferences the time
the countdown was started.

Using the alarm manager is also the only way you can make sure you execute
when the time expires, even if the user has turned off the phone.






-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by g1bb » Sat, 24 Jan 2009 02:31:43 GMT


 Thanks Dianne.








--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by John Bohumil » Sat, 24 Jan 2009 06:01:33 GMT


 >
As it happens I am writting a countdown timer that implements a repeating
notification at quite short interface.  For example the application might
chirp every 10 seconds.  This is used for certain breathing excercises where
you hold the breath for 10 seconds, breathe out for 10 seconds, in for 10
seconds etc.

For such short intervals would I still be better off using the alarm
manager?  The handler technique seems to work great for my purposes but I
want to make sure I write a well behaved app.

John

--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by Dianne Hackborn » Sat, 24 Jan 2009 06:14:07 GMT


 The alarm manager would probably be overkill for that.  It might make more
sense to do this in a Service, which holds a partial wake lock to keep
itself running.  Note that this will have a noticeably impact on battery
life because you are preventing the CPU from going to sleep, though I can't
tell you exactly how much.






-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by Mariano Kamp » Sat, 24 Jan 2009 18:01:52 GMT


 nd when implementing stuff as a service where would the actual long running
functionality go?
In a separate thread?
How should I stop that thread?

Is acting on Service.onDestroy() the right time and will it definitively be
called?
How much time do I have in onDestroy()?
Is it acceptable behavior to set a flag that the background thread should
stop and then wait for the thread signaling that it is finished?

To illustrate that, think of a Download Service that should download four
files.
When onDestroy() is called a flag shouldShutdown is set to true and
onDestroy() waits then for the thread to finish (Thread.join()).
The thread itself is in the midst of downloading the 2nd file and monitors
shouldShutdown and doesn't start a new download (if possible doesn't even
ask for the next bytes) when it is set, but cleans up resources (open
connections) and terminates itself.

Is that ok?

It would certainly not help the OS to quickly get rid of the service, at
least when the service is busy with a "long" lasting operation.

On Fri, Jan 23, 2009 at 6:45 PM, Dianne Hackborn <hack...@android.com>wrote:


--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by Guillaume Perrot » Sat, 24 Jan 2009 23:04:55 GMT


 f you want to stop a thread that is running long operations, you
should read this: http://g.oswego.edu/dl/cpj/cancel.html.
In your service onDestroy you should not "join" your thread, just
launch the cancel operation, the trick with all lifecycle methods
(including services) is that they are called in the main thread.
If you block on the main thread, you won't be able to start a new
activity from this process (the process is rarely stopped, and
relaunched automatically when a crash occurs), it will freeze and
you'll see the "sorry dialog" with the "wait" and "force close"
buttons.

On Jan 24, 11:01am, Mariano Kamp <mariano.k...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by Dianne Hackborn » Sun, 25 Jan 2009 05:23:46 GMT


 lso the question is backwards -- the point of a service is to tell the
system you have some background operation that needs to continue running.
You wouldn't stop the service until your thread is done with its work, so by
definition onDestroy() won't be called until that thread is done and thus
there is no need to wait for it.

On Sat, Jan 24, 2009 at 7:04 AM, Guillaume Perrot
<guillaume.p...@gmail.com>wrote:



--
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support. All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by mnj » Tue, 27 Jan 2009 14:18:17 GMT


 Hi,

I did a simple test where I start a service from an activity after
acquiring PARTIAL_WAKE_LOCK. Then a 100% charged phone will drain
completely in 10 hours when CPU is always on.

Regards,
Manoj




--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by blindfold » Sat, 31 Jan 2009 07:11:24 GMT


 aargh! Thank you for this post, as it led me to fix a memory leak
where my app would consume 50K more with every run. I was looking into
all sorts of context leaks, but it was just that threads are not
automatically killed when pressing the back button to "exit" the app,
so if one launches a thread in onCreate(), one adds about 10K per run,
and DDMS will show a growing pile of threads as the run count
increases...

Thanks

On Jan 24, 4:04pm, Guillaume Perrot <guillaume.p...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



When does my thread die?

by Mark Murphy » Sat, 31 Jan 2009 07:29:13 GMT


 


I try to use queues for controlling work done in background threads,
mostly because that's how I was controlling background threads in
pre-Android development. LinkedBlockingQueue is a great choice, though
there are plenty of others in java.util.concurrent.

(java.util.concurrent, by the way, written by Doug Lea, author of the
book and the extract of same that M. Perrot linked to)

So, in onDestroy(), I just pop my own KillEvent object on the queue(s)
and, once the queues get to that message, they terminate their own
threads simply by falling out of the queue-processing loop. So long as
your queues don't get backed up too deep, you should be OK, and I think
there's a priority queue implementation in java.util.concurrent as well
if your threads still aren't closing up shop fast enough.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 2.0 Published!

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Why i can't find the .db file in the data directory of my application?

Hi, all,
      I have written a test case to implement a conference contact
management system with Content Provider and SQLite. The launch
activity is a Listactivity and the customized provider is extended
from ContentProvider, u know, actually the sample is so similar to the
Notepad Sample in the official SDK packages. However, i can't find the
created .db file in the /data/data/myapp/ except the /lib directory.
Why?
      I can't find out the reason, any suggestion is appreciated!!!

--~--~---------~--~----~------------~-------~--~----~

2. populating a ListView in xml...

I want to create a list view with limited set of items that isn't
really going to change.

Each row is going to have an image on the left and right and text in
the middle.

Can I populate that in XML (i.e. avoid writing code) ?

        <ListView android:id="@+id/LimitedList"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:choiceMode="singleChoice">
        </ListView>

tia.
--~--~---------~--~----~------------~-------~--~----~

3. DevTools application not seen in Android 1.5

4. How to override ContactsProvider like GoogleContactsProvider

5. Where do we have to close the DB in a ContentProvider?

6. what permission to us "ContentResolver cr = getContentResolver();" ???

7. How to Make your owrn SDK with full calss library(function)