Not Quite As Painful Threading

1,760 views

Published on

from Apps World Europe 2013

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,760
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
43
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Not Quite As Painful Threading

  1. 1. Copyright © 2013 CommonsWare, LLC Painless Threading Apps World Europe 2013
  2. 2. Copyright © 2013 CommonsWare, LLC Painless Threading Apps World Europe 2013 Not Quite As Painful
  3. 3. Copyright © 2013 CommonsWare, LLC Problem #1: Jank ● Sluggish UI Response to User Input – Example: hiccups while scrolling a list ● Reason:Too MuchTime Spent on Main ApplicationThread – 60 fps UI updates 16 ms/frame need to→ → spend < 1ms per callback method ● Limitation: Cannot Modify UI from BackgroundThread
  4. 4. Copyright © 2013 CommonsWare, LLC Problem #2: Changes ● Configuration Changes – Example: rotate the device ● Default Behavior = Destroy/Recreate UI – Activities and (most) fragments ● WhatWe Do NotWant: Lost Results – Spin off a thread, which updates the former UI, not the current UI
  5. 5. Copyright © 2013 CommonsWare, LLC Problem #3: Sleep ● Devices Fall Asleep After Inactivity – Screen goes dark, CPU powers down – Can be in as little as 15 seconds ● Less if user presses POWER button directly ● WhatWe Do NotWant: IncompleteWork – Spin off thread to do network I/O, fall asleep mid-conversation, with messed up results
  6. 6. Copyright © 2013 CommonsWare, LLC Problem #4: Cores ● Good News!We Have Multiple Cores Now! – Better responsiveness from CPU-intensive apps, such as games ● Bad News!We Have Multiple Cores Now! – Thread safety now a bigger problem than before – Can have results ranging from outright crashes to subtle inconsistencies
  7. 7. Copyright © 2013 CommonsWare, LLC Timing Is Everything ● GeneralTransactional Recipe – 1 ms to 1 s use→ AsyncTask and retained fragment – 1-15 s use→ IntentService and event bus – 15+ s use wakeful pattern and event bus→ ● Also if the work is triggered outside the UI, such as via a push message
  8. 8. Copyright © 2013 CommonsWare, LLC AsyncTask ● QuickWork – Disk and database I/O – Trivial network I/O (e.g., downloading thumbnails) ● Android SuppliesThread Pool ● Android GivesYou Hook to Send Results to Main ApplicationThread
  9. 9. Copyright © 2013 CommonsWare, LLC SimpleAsyncTask ● https://gist.github.com/commonsguy/6900714 ● AsyncTask Subclass, Simpler API – Override doInBackground() and do the work that will take a moment ● Called on a framework-supplied background thread – Override onPostExecute() and update the UI based on the background work ● Called on main application thread once doInBackground() completes – Create instance, call execute()
  10. 10. Copyright © 2013 CommonsWare, LLC AsyncTask ● What AsyncTask Itself GivesYou – Optional parameters from execute() to doInBackground() – Optional results from doInBackground() passed to onPostExecute() – Additional hooks, like onPublishProgress() – Thread pool choices ● Cost: a bit of complexity
  11. 11. Copyright © 2013 CommonsWare, LLC Retained Fragment ● setRetainInstance(true) ● Effects – Fragment not destroyed and recreated on configuration change – onPostExecute() can work with fragment to update UI, will affect the appropriate widgets depending on timing
  12. 12. Copyright © 2013 CommonsWare, LLC Limits of AsyncTask ● Does Not Keep Device Awake ● Does Not Keep Process Alive ● Still Must Deal withThread Safety
  13. 13. Copyright © 2013 CommonsWare, LLC IntentService ● Use Case – Work that needs to be completed even if user leaves your UI (e.g., presses HOME)... – ...but not so long that the device is likely to fall asleep while you are doing that work ● Benefits – Lets Android know that you are doing work – Immune to configuration changes
  14. 14. Copyright © 2013 CommonsWare, LLC IntentService Recipe ● Extend IntentService – Override onHandleIntent(), where you do work on framework-supplied background thread ● Add <service> to Manifest ● Call startService() toTriggerWork – Takes Intent to identify the service – Package extras on Intent for work details – Intent supplied to onHandleIntent()
  15. 15. Copyright © 2013 CommonsWare, LLC Event Bus ● Loose Coupling for Result Delivery – LocalBroadcastManager ● Part of Android Support package – greenrobot's EventBus – Square's Otto ● Not as thread-savvy and so may not be suitable
  16. 16. Copyright © 2013 CommonsWare, LLC Event Bus Recipe ● UI Layer Receives Events – Register in onResume(), unregister in onPause() ● IntentService Sends Events – Picked up by UI layer if in foreground, to do what's needed – If not picked up, detect and raise a Notification, if appropriate
  17. 17. Copyright © 2013 CommonsWare, LLC Limits of IntentService ● No Direct UI Access – Message-based for work requests and results ● Does Not Keep Device Awake ● Still Must Deal withThread Safety – IntentService itself is single-threaded, so multiple commands are not a problem – ...but may still have other code accessing same data on main application thread, other threads
  18. 18. Copyright © 2013 CommonsWare, LLC WakefulIntentService ● http://github.com/commonsguy/cwac-wakeful ● Keeps Device Awake DuringWork ● Recipe – Download JAR – Extend WakefulIntentService instead of IntentService – Override doWakefulWork() instead of onHandleIntent() – Trigger work via sendWakefulWork()
  19. 19. Copyright © 2013 CommonsWare, LLC WakefulBroadcastReceiver ● Alternative to WakefulIntentService – Ships with Android Support package ● Recipe – Have work be triggered by broadcast to a subclass of WakefulBroadcastReceiver – Use startWakefulService() in onReceive() – IntentService calls completeWakefulIntent() when work done
  20. 20. Copyright © 2013 CommonsWare, LLC Limits ofWakeful Approaches ● No Direct UI Access – Still using event bus for delivering results ● Still Must Deal withThread Safety ● Power Consumption – Require WAKE_LOCK permission – You may appear in “battery blame screen” in Settings
  21. 21. Copyright © 2013 CommonsWare, LLC Recap ● GeneralTransactional Recipe – 1 ms to 1 s use→ AsyncTask and retained fragment – 1-15 s use→ IntentService and event bus – 15+ s use wakeful pattern and event bus→ ● Also if the work is triggered outside the UI, such as via a push message
  22. 22. Copyright © 2013 CommonsWare, LLC Code! ● https://github.com/commonsguy/cw-omnibus ● SimpleAsyncTask/Retained Fragment – /Threads/SimpleAsyncTask ● AsyncTask/Retained Fragment – /Threads/AsyncTask ● WakefulIntentService/Event Bus – /EventBus

×