I'm OK with having people who aren't very experienced with threads create a 
new AsynchTask for each recurrence. Better than debugging a synchronization 
problem -- or worse, living with it. And it's not really that high an 
overhead, either in code complexity or time and memory. It's not like it's 
creating a new thread each time.

Just to be clear: Handler/Looper doesn't give you threads. I've seen people 
get very confused on this point! It does make using threads, well, not 
simpler, but safer. If you're interested in passing off work to another 
thread, then it's better to use Handler/Looper than to try to handle the 
synchronization of passing off the work item yourself.

Still, since you have to create that work item to pass it off, generally 
speaking, you might as well make it an AsynchTask, and get thread pooling, 
clean interface to the UI thread, and convenient progress bar handling for 
free. The only time I can think of to prefer Handler/Looper is when the 
thread pooling behind AsynchTask doesn't fit your needs -- and if it 
doesn't, you may want to consider your needs, as having lots of threads 
takes lots of memory (for the stacks).

On Sunday, May 8, 2011 4:33:44 PM UTC-7, Indicator Veritatis wrote:
>
> All the answers I have seen so far are true, but yours is the most 
> informative. I would be a little careful about the generalization 
> though, that if he has to ask, then AsyncTask is the answer. AsyncTask 
> is not so convenient for recurring tasks, unless you are OK with 
> having to start it up all over again each time the trigger occurs. 
>
> And there is one other tool in Android that is still useful for 
> Threads, when barebones Java Threads are too much of a bother to 
> manage: Looper/Handler. This was the standard Android threading 
> 'workhorse' before AsyncTask (or its unoficcial predecessor UserTask) 
> became so popular. 
>
> On May 7, 11:31 am, Bob Kerns <[email protected]> wrote: 
> > Use an AsyncTask -- unless both of the following are true: 
> > 
> >    - AsyncTask doesn't meet your needs somehow 
> >    - You know very clearly and well how to properly synchronize between 
> >    threads, and how to transfer work to the UI thread where needed, etc. 
> > 
> > If AsyncTask doesn't meet your needs, and you don't have the second 
> > requirement -- you have a very steep (and error-prone) learning curve 
> ahead 
> > of you. The whole idea of AsyncTask is to make it easier to write 
> reliable 
> > asynchronous code. 
> > 
> > Generally speaking -- if you have to ask, AsyncTask is probably the 
> answer. 
> > 
> > On Saturday, May 7, 2011 8:53:29 AM UTC-7, Droid wrote: 
> > 
> > > Seems I can use an async task OR roll my own thread to do a background 
> > > task. 
> > 
> > > Is one better than the other?

-- 
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