Download presentation
Presentation is loading. Please wait.
1
Android Programming Lecture 8
Testing notes Multi-threading
2
Timesharing Operating Systems
All modern operating systems are capable of sharing processor(s) between multiple users and processes Process scheduling algorithms There are often lots (hundreds) of processes running Many are managed by the system Some are user executed
3
Process A process is typically defined as execution of a single program on a computing device Processes are scheduled by the OS Processes are owned by a user or the OS (root) Processes may have a long lifetime (a web server) or a short lifetime (one run of a short program) Processes do not share memory between each other Most OS support inter-process communication
4
Threads A thread is a component of a process
Usually considered the smallest unit that can be scheduled by an OS Can share memory and instructions with other threads in the same process Most OS support inter-thread communication
5
Process V.S. Thread
6
Timesharing Operating Systems
On a single processor system, “Round robin” scheduling system switches between threads and processes fast enough that it appears that multiple programs are running Only one is every truly executing at a time On multi-core or multi-processor systems, Threads or processes can be allocated across the processors True parallel processing “Round-robin” scheduling still occurs
7
Android Android starts a new Linux process for the application with a single thread of execution All components of the same application run in the same process and thread This thread is called the “main” thread or “UI” thread Any updates to the UI/display must be accomplished on the main thread
8
Slow Android App If a component of the work takes a long time, the rest of the work will be “blocked” For example, a long time to access data across the network prevents responding to any GUI events In the Android OS, if a GUI doesn’t respond to an input event in < five seconds, then it is considered unresponsive and the OS will try to kill it!
9
Multi-threading Put non-UI work on a separate thread
For example, download an image, connect to a remote server, open a web browser… Android App UI Main Thread Java Threads BG Background Thread
10
What technique shall I use for my background thread execution?
UI Android App Java Threads BG What technique shall I use for my background thread execution? Linux Process UI BG Application Start Application End Time
11
Asynchronous Techniques
Thread Executor HandlerThread AsyncTask Service IntentService AsyncQueryHandler Loader
12
Handler – Loop - Message
Message Queue UI Thread Message 1 Thread 1 sendMessage() Message 2 Looper Handler Message 3 Message 4 Thread 2
13
Handler UI Thread Message Queue Message 1 @Override protected void onCreate(Bundle savedInstanceState) { … final Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0x123) { // Do the things when the message } } }; final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // Open a new thread } }, 0, 100); Thread 1 Looper UI Thread Thread 1 @Override public void run() { // To be executed in the thread … } handler.sendEmptyMessage(0x123);
14
Timer Thread A thread manages a Runnable object Timer class
The java.util.Timer class provides facility for threads to schedule tasks for future execution in a background thread. This class schedules tasks for one-time execution, or for repeated execution at regular intervals. The Timer class in the java.util package schedules instances of a class called TimerTask final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // To be executed in the thread } }, 0, 100); Timer Class Reference: Runnable Class Reference:
15
Handler Allow you to send and process Message and Runnable objects associated with a thread’s Message Queue Handler instance is associated with a single thread and that thread’s message queue When you create a new Handler, it is bound to the thread and message queue of the thread that is creating it It will deliver Message and Runnable to that message queue and execute them as they come out of the message queue Need only one Handler object per activity Handler Message Queue Message 1 Looper
16
Handler Loop Create and Start 1 Message Queue Add Process 3 2 1 2 3
onCreate(){ … } 1 Handler h = new Handler() { @Override public void handleMessage(Message msg) { //Process message } }; 2 h.sendEmptyMessage(msg); 3
17
Message Class Defines a message containing a description and arbitrary data object that can be sent to a Handler. A message contains two extra int fields and an extra object field what: User-defined message code so that the recipient can identify what this message is about. obj: An arbitrary object to send to the recipient. While the constructor of Message is public, the best way to get one of these is to call Message.obtain() or one of the Handler.obtainMessage() methods, which will pull them from a pool of recycled objects.
18
Handler UI Thread Message Queue Message 1 @Override protected void onCreate(Bundle savedInstanceState) { … final Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0x123) { // Do the things when the message } } }; final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // Open a new thread } Thread 1 Looper UI Thread Thread 1 @Override public void run() { // To be executed in the thread … handler.sendEmptyMessage(0x123); }
19
Handler Methods to Send Message
sendEmptyMessage(int what) Sends a Message containing only the what value. sendEmptyMessageDelayed(int what, long delayMillis) Sends a Message containing only the what value, to be delivered at a specific time. sendEmptyMessageAtTime(int what, long uptimeMillis) Sends a Message containing only the what value, to be delivered after the specified amount of time elapses. sendMessage(Message msg) Pushes a message onto the end of the message queue after all pending messages before the current time. sendMessageAtFrontOfQueue(Message msg) Enqueue a message at the front of the message queue, to be processed on the next iteration of the message loop. sendMessageAtTime(Message msg, long uptimeMillis) Enqueue a message into the message queue after all pending messages before the absolute time (in milliseconds) uptimeMillis.
20
References: Android Programming: The Big Nerd Ranch Guide
Chapter 24 (Safari Books Online)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.