Mobile Application Development Produced David Drohan ([email protected]) by Department of Computing & Mathematics Waterford Institute of Technology http://www.wit.ie Android Multithreading " & Networking Android Multithreading & Networking 2! Agenda & Goals ❑ Understand what multithreading is and the need for it in Android development ❑ Be aware of the different approaches available in Android multithreading ❑ Be aware how to implement some of these approaches (including AsyncTasks) in an Android app. ❑ Understand JSON & Googles Gson ❑ Investigate the use of Volley in Android Networking ❑ Revisit our Donation Case Study Android Multithreading & Networking 3! Background Processes in General ❑ One of the key features of Android (and iPhone) is the ability to run things in the background Threads ■ Run something in the background while user interacts ⬥ with UI Services ■ Regularly or continuously perform actions that don’t ⬥ require a UI Android Multithreading & Networking 4! Threads ❑ Recall that Android ensures responsive apps by enforcing a 5 second limit on Activities ❑ Sometimes we need to do things that take longer than 5 seconds, or that can be done while the user does something else ❑ Activities, Services, and Broadcast Receivers run on the main application thread ❑ But we can start background/child threads to do other things for us Android Multithreading & Networking 5! Threads h"p://developer.android.com/reference/java/lang/Thread.html q A Thread is a concurrent unit of execution. q Each thread has its own call stack. The call stack is used on method calling, parameter passing, and storage for the called method’s local variables. q Each virtual machine instance has at least one main thread." q Threads in the same VM interact and synchronize by the use of shared objects and monitors associated with these objects. Android Multithreading & Networking 6! Threads Process 1 (Virtual Machine 1) Process 2 (Virtual Machine 2) Common memory resources Common memory resources Main thread Thread-2 main Thread-1 thread Android Multithreading & Networking 7! Android Thread Constraints ❑ Child threads cannot access UI elements (views); these elements must (and can only) be accessed through the main thread ❑ So what do you do? You give results to the main thread and let it use the ■ results Android Multithreading & Networking 8! Advantages of Multithreading ❑ Threads share the process' resources but are able to execute independently. ❑ Applications responsibilities can be separated main thread runs UI, and ■ slow tasks are sent to background threads. ■ ❑ Threading provides an useful abstraction of concurrent execution. ❑ A multithreaded program operates faster on computer systems that have multiple CPUs. " (Java 8 supports multi-core multi-threading) Android Multithreading & Networking 9! Disadvantages ! ! q Code tends to be more complex A1 q Need to detect, avoid, resolve A3 deadlocks A2 A1 A2 ! ! WaiBng for A2 WaiBng for A1 to finish to finish Android Multithreading & Networking 10!
Description: