Tying Up Loose Threads

 

A common pattern in Android is to perform some work on a background thread, then update something on the UI thread when the background work is complete. A simple way to handle this is to fork a background thread and use a or to accomplish the UI thread work. However, the danger here is in forking too many threads – the user might do something that causes you to fork many threads in short order, bogging down the device at best. And while there are classes that can help you manage a work pool (e.g., ), their use can be mildly tedious.

Enter .

manages a work queue with a thread pool, so you do not need to implement that yourself. Moreover, all of the communication between foreground and background threads are handled for you. All you need to do is override a few methods to describe what you want done in the background or in the foreground.

To create an , simply extend one anonymously, like you might do to create a . You can then override whatever methods you need to for the pattern you want:

• Override to specify something that should be done on the UI thread when the task is started

• Override to indicate the work that should be done in the background thread

• Override to update your progress on the UI thread when calls (e.g., update a )

• Override to do whatever work needs to be done on the UI thread after the background work is complete

Using your custom is then a matter of calling on one of its instances, such as one might call on a .

 








Дата добавления: 2015-05-16; просмотров: 766;


Поиск по сайту:

При помощи поиска вы сможете найти нужную вам информацию.

Поделитесь с друзьями:

Если вам перенёс пользу информационный материал, или помог в учебе – поделитесь этим сайтом с друзьями и знакомыми.
helpiks.org - Хелпикс.Орг - 2014-2024 год. Материал сайта представляется для ознакомительного и учебного использования. | Поддержка
Генерация страницы за: 0.003 сек.