Android developers blog asynctask


Painless threads

If you begin an Android application, a thread known as “primary” is instantly produced. The primary thread, also known as the UI thread, is essential since it manages dispatching the occasions towards the appropriate icons including enter occasions. It’s also the thread you communicate with Android icons on. For example, should you touch the a control button on-screen, the UI thread dispatches the touch event towards the widget which sets its pressed condition and posts an invalidate request towards the event queue. The UI thread dequeues the request and notifies the widget to redraw itself.

This single thread model can yield poor performance in Android programs that don’t think about the implications. Since everything happens on one thread carrying out lengthy procedures, like network access or database queries, about this thread will block the entire interface. No event could be sent, including drawing occasions, as the lengthy operation is going ahead. In the user’s perspective, the applying seems hung.

If you wish to observe how bad this could look, write an easy application having a button that creates Thread. The button will stay in the pressed condition for around 2 seconds before returning to the normal condition. At these times, it’s very simple for the consumer to see the applying as slow.

Now you know you must avoid extended procedures around the UI thread, you’ll most likely use extra threads (background or worker threads) to do these procedures, and appropriately so.

public void onClick(View v) 

In the beginning, this code appears to become a good means to fix your condition, as it doesn’t block the UI thread. Regrettably, it violates the only thread model: the Android UI toolkit isn’t thread-safe and should continually be altered around the UI thread. Within this bit of code, the ImageView is altered on the worker thread, which could cause really strange problems. Searching for and fixing such bugs can be challenging and time-consuming.

Android offers a number of ways to gain access to the UI thread using their company threads. You might already know about a number of them but this is a comprehensive list:

These classes and techniques could be employed to correct our previous code example:

public void onClick(View v) publish(new Runnable() 


Regrettably, these classes and techniques also makes your code more difficult and much more hard to read. It might be a whole lot worse whenever your implement complex procedures that need frequent UI updates. To treat this issue, Android 1.5 provides a new utility class, known as AsyncTask, that simplifies the development of lengthy-running tasks that require to talk with the interface.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>