Lecture 6 Process and Threads Topics Process Threads

  • Slides: 14
Download presentation
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar

Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016

References (study these) • http: //developer. android. com/guide/components/processes-and-threads. html

References (study these) • http: //developer. android. com/guide/components/processes-and-threads. html

Process • A program is a set of instructions + data stored in an

Process • A program is a set of instructions + data stored in an executable image. (passive) • A process is a program “in action” (dynamic) • • • Program counter CPU registers Stacks (parameters, return addresses, saved variables) States – running, waiting, stopped, zombie. Runs in own virtual address space. Uses secure, kernel managed mechanism to interact with other processes. For more info: http: //www. tldp. org/LDP/tlk/kernel/processes. html

Process vs. Thread • Thread = a flow of execution inside a process. •

Process vs. Thread • Thread = a flow of execution inside a process. • Threads in a process share the same virtual memory address space. • Context switching between threads are faster than context switching between processes. • 1: 1 mapping between kernel space and user space threads.

Android Processes and Threads • Default Behavior: • All components of an App run

Android Processes and Threads • Default Behavior: • All components of an App run in the same process and thread (the main/UI thread). • However, you can: • Arrange for components to run in different processes. • Create multiple threads per process. C 1 C 2 C 3 T 1 T P Default C 2 C 1 T 2 P 1 It’s possible P 2

android: process • You can specify android: process in the Android Manifest file for:

android: process • You can specify android: process in the Android Manifest file for: • <activity>, <service>, <receiver>, <provider> • <application> (all components; same UID and certificate)

Process – creation and removal • Creation: When the first component of an App

Process – creation and removal • Creation: When the first component of an App is run and there is currently no process for that App. • Removal: Remove old processes to reclaim memory for new or more important processes. Which process should be killed?

Process Hierarchy • From High Priority to Low Priority 1. Foreground (highest priority, interactions

Process Hierarchy • From High Priority to Low Priority 1. Foreground (highest priority, interactions on-going, can be Activity, Service, Broadcast receiver) 2. Visible (not fore ground but visible) 3. Service (start. Service, playing music/downloads) 4. Background (on. Stop called, no visible, LRU kill) 5. Empty (lowest priority) Kill them first Visible Activity Classified as Visible Process Service P

Main or UI Thread • Created when App is launched • Often called UI

Main or UI Thread • Created when App is launched • Often called UI thread • Dispatched events to widgets • Your App interacts with components from android. widget android. view package Caution: Don’t run long running (5 sec+) task in the UI thread

Worker Thread • Use these to make UI thread light-weight, responsive. • Limitation: Never

Worker Thread • Use these to make UI thread light-weight, responsive. • Limitation: Never access UI toolkit elements (e. g. views) from another thread. Runs in UI Thread Runs in the new worker Thread public void on. Click(View v) { new Thread(new Runnable() { public void run() { Bitmap b = load. Image. From. Network("http: //example. com/i. png" ); m. Image. View. set. Image. Bitmap(b); } }). start(); } Do not try this (i. e. accessing an Image. View which was created in UI thread and now being accessed in a worker thread)

So … two rules to remember • Do not block the UI thread Caution:

So … two rules to remember • Do not block the UI thread Caution: Don’t run long running (5 sec+) task in the UI thread • Do not access the Android UI toolkit from outside the UI thread.

Access UI Thread from Other Threads • Use one of these three: • Activity.

Access UI Thread from Other Threads • Use one of these three: • Activity. run. On. Ui. Thread(Runnable) • View. post. Delayed(Runnable, long) Runs in UI Thread Runs in worker Thread public void on. Click(View v) { new Thread(new Runnable() { public void run() { final Bitmap bitmap = load. Image. From. Network("http: //example. com/image. png"); m. Image. View. post(new Runnable() { public void run() { m. Image. View. set. Image. Bitmap(bitmap); } }). start(); }

Async Task • Performs the blocking operations in a worker thread, and publishes the

Async Task • Performs the blocking operations in a worker thread, and publishes the results on the UI thread. public void on. Click(View v) { new Download. Image. Task(). execute("http: //example. com/image. png"); } private class Download. Image. Task extends Async. Task<String, Void, Bitmap> { /** The system calls this to perform work in a worker thread and * delivers it the parameters given to Async. Task. execute() */ protected Bitmap do. In. Background(String. . . urls) { Runs in worker return load. Image. From. Network(urls[0]); Thread } /** The system calls this to perform work in the UI thread and delivers * the result from do. In. Background() */ protected void on. Post. Execute(Bitmap result) { Runs in UI m. Image. View. set. Image. Bitmap(result); Thread } } http: //developer. android. com/reference/android/os/Async. Task. html

Thread-safe methods and IPC • Make sure, when multiple threads can invoke a method,

Thread-safe methods and IPC • Make sure, when multiple threads can invoke a method, the method is thread-safe. Content Provider Query(). Insert(), delete(), update() Another Activity • Android offers inter-process communication using remote procedure calls (RPCs)