Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Android Threading
Charlie Tsai @ Android Study Group
Charlie Tsai @ Android Study Group
cha122977@gmail.com
Software Engineer
Today’s source code
throws new NullPointerExcepti...
Agenda
• Background Knowledge
• Android Thread
• Free Discussions :P
Background
Knowledge
What is Thread?
• Process
• Process vs Thread
• Thread and Process of JVM
• Multithreading vs Multi-process
• Kernel Task
Issues of Multithreading
• Race condition (Synchronization issue)
• Dead lock issue
• Interaction of Thread
• Hard to test...
Android Thread
Android Thread
• UI Thread (==Main Thread) & Worker Thread
• Looper, MessageQueue, and Handler
• UI Refreshing
• ActivityT...
Useful APIs
• View.post() & runOnUiThread()
• Thread & Runnable
• Executor & Runnable
• Future & Callable
• Handler & Hand...
View.post() &
runOnUiThread()
• Pros
• Easy to use
• Make function adaptive
• Cons
• May cause callback hell
Thread & Runnable
• Pros
• Easy to use
• Fully control
• Cons
• Fully control
• No return value
• Data synchronization iss...
Executor & Runnable
• Pros
• Easy to use
• Avoid too much instances of Thread
• Cons
• No return value
Future & Callable
• Pros
• Easy to use
• Have return value
• Cons
• Get return value may block UI Thread
Bad Usage
Handler & HandlerThread
• Pros
• Easy to use
• Easy to communication between threads
• Cons
• Need to implement switch-cas...
AsyncTask
• Pros
• Progress Updating
• Easy to cancel
• onPostExecute run in UI Thread
• Cons
• Duplicated codes
• Hard to...
IntentService
• Pros
• Easy to use
• Not depends on Activity
• Cons
• No return value
• Limitation of data type
AsyncLoader
• Pros
• Easy to manager multiple Loaders
• Asynchronous
• Clear to read
• Cons
• Need to implement callback i...
Free Discussions! :P
ResultReceiver
• Implement Parcelable
• Can be used to return values
• Use Bundle to carry the data
• Can send data multip...
Request Side
Return (Result) Side
Android Multiple
Processes
Enable Multiple Processes
• android:process=“:newProcessName”
• startService() and/or bindService()
IPC & RFC
• Inter-Process Communication
• Remote Function Call (*RemoteException)
• ContentProvider & ContentReslover
• Bo...
Parcel
Parcel
• Why Parcel?
• Parcelable
• AIDL
• Bundle.putParcelable()
• Message.obj
工商時間的說
December 3rd @ 台灣大學普通教學館
固定聚會:每週三@果子咖啡(近捷運南京復興站)
技術分享:每月底週六14:30 - 17:30
以及眾多不定期活動(ex: Workshop)
Questions?
Thanks!
Android Thread
Android Thread
Android Thread
Android Thread
Android Thread
Upcoming SlideShare
Loading in …5
×

Android Thread

1,851 views

Published on

Presentation at Android Taipei on 2016/11/24(Thur.)

Published in: Software

Android Thread

  1. 1. Android Threading Charlie Tsai @ Android Study Group
  2. 2. Charlie Tsai @ Android Study Group cha122977@gmail.com Software Engineer Today’s source code throws new NullPointerException()
  3. 3. Agenda • Background Knowledge • Android Thread • Free Discussions :P
  4. 4. Background Knowledge
  5. 5. What is Thread? • Process • Process vs Thread • Thread and Process of JVM • Multithreading vs Multi-process • Kernel Task
  6. 6. Issues of Multithreading • Race condition (Synchronization issue) • Dead lock issue • Interaction of Thread • Hard to testing
  7. 7. Android Thread
  8. 8. Android Thread • UI Thread (==Main Thread) & Worker Thread • Looper, MessageQueue, and Handler • UI Refreshing • ActivityThread.java • ViewRootImpl.java • Choreographer.java
  9. 9. Useful APIs • View.post() & runOnUiThread() • Thread & Runnable • Executor & Runnable • Future & Callable • Handler & HanderThread • AsyncTask • IntentService • AsyncLoader
  10. 10. View.post() & runOnUiThread() • Pros • Easy to use • Make function adaptive • Cons • May cause callback hell
  11. 11. Thread & Runnable • Pros • Easy to use • Fully control • Cons • Fully control • No return value • Data synchronization issue
  12. 12. Executor & Runnable • Pros • Easy to use • Avoid too much instances of Thread • Cons • No return value
  13. 13. Future & Callable • Pros • Easy to use • Have return value • Cons • Get return value may block UI Thread
  14. 14. Bad Usage
  15. 15. Handler & HandlerThread • Pros • Easy to use • Easy to communication between threads • Cons • Need to implement switch-case of Handler • Limitation of data type
  16. 16. AsyncTask • Pros • Progress Updating • Easy to cancel • onPostExecute run in UI Thread • Cons • Duplicated codes • Hard to trace code
  17. 17. IntentService • Pros • Easy to use • Not depends on Activity • Cons • No return value • Limitation of data type
  18. 18. AsyncLoader • Pros • Easy to manager multiple Loaders • Asynchronous • Clear to read • Cons • Need to implement callback interface • Only for Activity & Fragment • Hard to use
  19. 19. Free Discussions! :P
  20. 20. ResultReceiver • Implement Parcelable • Can be used to return values • Use Bundle to carry the data • Can send data multiple times
  21. 21. Request Side
  22. 22. Return (Result) Side
  23. 23. Android Multiple Processes
  24. 24. Enable Multiple Processes • android:process=“:newProcessName” • startService() and/or bindService()
  25. 25. IPC & RFC • Inter-Process Communication • Remote Function Call (*RemoteException) • ContentProvider & ContentReslover • Bound service + Messenger • Bound service + AIDL
  26. 26. Parcel
  27. 27. Parcel • Why Parcel? • Parcelable • AIDL • Bundle.putParcelable() • Message.obj
  28. 28. 工商時間的說
  29. 29. December 3rd @ 台灣大學普通教學館
  30. 30. 固定聚會:每週三@果子咖啡(近捷運南京復興站) 技術分享:每月底週六14:30 - 17:30 以及眾多不定期活動(ex: Workshop)
  31. 31. Questions?
  32. 32. Thanks!

×