Android Chapter13 Multi Threading
Android Chapter13 Multi Threading
Android
Multi-Threading
Victor Matos
Cleveland State University
Multi-Threading
Threads http://developer.android.com/reference/java/lang/Thread.html
2. It thread has its own call stack for methods being invoked, their arguments
and local variables.
3. Each virtual machine instance has at least one main Thread running when it
is started; typically, there are several others for housekeeping.
2
13. Android – Multi-Threading
Multi-Threading
Threads http://developer.android.com/reference/java/lang/Thread.html
Threads in the same VM interact and synchronize by the use of shared objects
and monitors associated with these objects.
There are basically two main ways of having a Thread execute application code.
In both cases, the start() method must be called to actually execute the
new Thread.
3
13. Android – Multi-Threading
Multi-Threading
Process 2 (Dalvik Virtual Machine 2)
Process 1 (Dalvik Virtual Machine 1)
Main
thread main
thread
Thread-2
Thread-1
4
13. Android – Multi-Threading
Multi-Threading
Advantages of Multi-Threading
1. Threads share the process' resources but are able to execute independently.
2. Applications responsibilities can be separated
• main thread runs UI, and
• slow tasks are sent to background threads.
5
13. Android – Multi-Threading
Multi-Threading
Disadvantages of Multi-Threading
1. Code tends to be more complex
6
13. Android – Multi-Threading
Multi-Threading
7
13. Android – Multi-Threading
Multi-Threading
Handler Class
http://developer.android.com/reference/android/os/Handler.html
• When a process is created for your application, its main thread is dedicated
to running a message queue that takes care of managing the top-level
application objects (activities, intent receivers, etc) and any windows they
create.
• You can create your own secondary threads, and communicate back with
the main application thread through a Handler.
• When you create a new Handler, it is bound to the message queue of the
thread that is creating it -- from that point on, it will deliver messages and
runnables to that message queue and execute them as they come out of the
message queue.
8
13. Android – Multi-Threading
Multi-Threading
Handler Class
http://developer.android.com/reference/android/os/Handler.html
9
13. Android – Multi-Threading
Multi-Threading
Threads and UI
Warning
Background threads are not allowed to interact with the UI.
Only the main process can access the (main) activity’s view.
10
13. Android – Multi-Threading
Multi-Threading
Handler‘s MessageQueue
A secondary thread that wants to communicate with the main thread must
request a message token using the obtainMessage() method.
Once obtained, the background thread can fill data into the message token and
attach it to the Handler’s message queue using the sendMessage() method.
A message extracted from the process’ queue can either return some data to the
main process or request the execution of runnable objects through the post()
method.
11
13. Android – Multi-Threading
Multi-Threading
12
13. Android – Multi-Threading
Multi-Threading
Using Messages
Main Thread Background Thread
... ...
Handler myHandler = new Handler() { Thread backgJob = new Thread (new Runnable (){
@Override @Override
public void handleMessage(Message msg) { public void run() {
//...do some busy work here ...
v the message...
// do something with //get a token to be added to
// update GUI if needed! //the main's message queue
... Message msg = myHandler.obtainMessage();
}//handleMessage ...
//deliver message to the
};//myHandler //main's message-queue
myHandler.sendMessage(msg);
... }//run
});//Thread
13
13. Android – Multi-Threading
Multi-Threading
Using Post
Main Thread Background Thread
...
Handler myHandler = new Handler(); // this is the "Runnable" object
@Override // that executes the background thread
public void onCreate(
Bundle savedInstanceState) { private Runnable backgroundTask
... = new Runnable () {
Thread myThread1 = @Override
new Thread(backgroundTask, public void run() {
"backAlias1"); ... Do some background work here
myThread1.start(); myHandler.post(foregroundTask);
}//onCreate }//run
... };//backgroundTask
//this is the foreground runnable
private Runnable foregroundTask
= new Runnable() {
@Override
public void run() {
// work on the UI if needed
}
...
14
13. Android – Multi-Threading
Multi-Threading
Messages
To send a Message to a Handler, the thread must first invoke obtainMessage() to
get the Message object out of the pool.
There are a few forms of obtainMessage(), allowing you to just create an empty
Message object, or messages holding arguments
Example
// thread 1 produces some local data
String localData = “Greeting from thread 1”;
// thread 1 requests a message & adds localData to it
Message mgs = myHandler.obtainMessage (1, localData);
15
13. Android – Multi-Threading
Multi-Threading
sendMessage Methods
You deliver the message using one of the sendMessage...() family of methods,
such as …
Multi-Threading
Processing Messages
To process messages sent by the background threads, your Handler needs to
implement the listener
handleMessage()
which will be called with each message that appears on the message queue.
There, the handler can update the UI as needed. However, it should still do that
work quickly, as other UI work is suspended until the Handler is done.
17
13. Android – Multi-Threading
Multi-Threading
Example 1. Progress Bar – Using Message Passing
The main thread displays a horizontal and a circular progress bar widget showing
the progress of a slow background operation. Some random data is periodically sent
from the background thread and the messages are displayed in the main view.
<?xml version="1.0" encoding="utf-8"?> <TextView
<LinearLayout android:id="@+id/TextView02"
android:id="@+id/widget28" android:layout_width="fill_parent"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_height="fill_parent" android:text="returned from thread..."
android:background="#ff009999" android:textSize="14sp"
android:orientation="vertical" android:background="#ff0000ff"
xmlns:android="http://schemas.android.com/apk/res/android" android:textStyle="bold"
> android:layout_margin="7px"/>
<TextView
android:id="@+id/TextView01" </LinearLayout>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Working ...."
android:textSize="18sp"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" />
<ProgressBar
android:id="@+id/progress2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
18
13. Android – Multi-Threading
Multi-Threading
Example 1. Progress Bar – Using Message Passing
// Multi-threading example using message passing
package cis493.threads;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
19
13. Android – Multi-Threading
Multi-Threading
Example 1. Progress Bar – Using Message Passing
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
//do something with the value sent by the background thread here ...
msgReturned.setText("returned by background thread: \n\n"
+ returnedValue);
bar1.incrementProgressBy(2);
Multi-Threading
Example 1. Progress Bar – Using Message Passing
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
msgWorking = (TextView)findViewById(R.id.TextView01);
msgReturned = (TextView)findViewById(R.id.TextView02);
}//onCreate
super.onStop();
isRunning = false;
}
21
13. Android – Multi-Threading
Multi-Threading
Example 1. Progress Bar – Using Message Passing
public void onStart() {
super.onStart();
// bar1.setProgress(0);
Thread background = new Thread(new Runnable() {
public void run() {
try {
for (int i = 0; i < MAX_SEC && isRunning; i++) {
//try a Toast method here (will not work!)
//fake busy busy work here
Thread.sleep(1000); //one second at a time
Random rnd = new Random();
Multi-Threading
24
13. Android – Multi-Threading
Multi-Threading
Example2. Using Handler post(...) Method
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff009999"
android:orientation="vertical"
xmlns:android=http://schemas.android.com/apk/res/android >
<TextView
android:id="@+id/lblTopCaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2px"
android:text="Some important data is been collected now. Patience please..."
android:textSize="16sp"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/myBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/txtBox1"
android:layout_width="fill_parent"
android:layout_height="78px"
android:layout_marginLeft="20px"
android:layout_marginRight="20px"
android:textSize="18sp" android:layout_marginTop="10px" />
<Button
android:id="@+id/btnDoSomething"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4px"
android:layout_marginLeft="20px"
android:text="Do Something" />
</LinearLayout>
25
13. Android – Multi-Threading
Multi-Threading
Example2. Using Handler post(...) Method
// using Handler post(...) method to execute foreground runnables
package cis493.threads;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
ProgressBar myBar;
TextView lblTopCaption;
EditText txtBox1;
Button btnDoSomething;
int accum = 0;
long startingMills = System.currentTimeMillis();
String PATIENCE = "Some important data is been collected now. " +
"\nPlease be patient. “;
26
13. Android – Multi-Threading
Multi-Threading
Example2. Using Handler post(...) Method
Handler myHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lblTopCaption = (TextView)findViewById(R.id.lblTopCaption);
btnDoSomething = (Button)findViewById(R.id.btnDoSomething);
btnDoSomething.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Editable txt = txtBox1.getText();
Toast.makeText(getBaseContext(), "You said >> " + txt, 1).show();
}//onClick
});//setOnClickListener
}//onCreate
27
13. Android – Multi-Threading
Multi-Threading
Example2. Using Handler post(...) Method
@Override
protected void onStart() {
super.onStart();
// create background thread were the busy work will be done
Thread myThread1 = new Thread(backgroundTask, "backAlias1" );
myThread1.start();
myBar.incrementProgressBy(0);
}
Multi-Threading
Example2. Using Handler post(...) Method
//this is the "Runnable" object that executes the background thread
private Runnable backgroundTask = new Runnable () {
@Override
public void run() {
//busy work goes here...
try {
for (int n=0; n<20; n++) {
//this simulates 1 sec. of busy activity
Thread.sleep(1000);
//now talk to the main thread
myHandler.post(foregroundTask);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}//run
};//backgroundTask
}//ThreadsPosting
29
13. Android – Multi-Threading
Multi-Threading
Thread States
Android‘s threads run
in a manner similar to
common Java threads
Thread.State Description
BLOCKED The thread is blocked and waiting for a lock.
NEW The thread has been created, but has never been
started.
RUNNABLE The thread may be run.
TERMINATED The thread has been terminated.
TIMED_WAITING The thread is waiting for a specified amount of time.
30
WAITING The thread is waiting.
31
13. Android – Multi-Threading
Multi-Threading
Using the AsyncTask class
1. AsyncTask enables proper and easy use of the UI thread.
2. This class allows to perform background operations and publish results on the
UI thread without having to manipulate threads and/or handlers.
Multi-Threading
Using the AsyncTask class
1. AsyncTask enables proper and easy use of the UI thread.
2. This class allows to perform background operations and publish results on the
UI thread without having to manipulate threads and/or handlers.
33
13. Android – Multi-Threading
Multi-Threading
Using the AsyncTask class
private class VerySlowTask extends AsyncTask<String, Long, Void> {
}
// this is the SLOW background thread taking care of heavy tasks
// cannot directly change UI
protected Void doInBackground(final String... args) {
... publishProgress((Long) someLongValue);
}
// periodic updates - it is OK to change UI
@Override
protected void onProgressUpdate(Long... value) {
}
// End - can use UI thread here
protected void onPostExecute(final Void unused) {
}
}
34
13. Android – Multi-Threading
Multi-Threading
AsyncTask <Params, Progress, Result>
Params: the type of the parameters sent to the task upon execution.
Progress: the type of the progress units published during the background
computation.
Result: the type of the result of the background computation.
Not all types are always used by an asynchronous task. To mark a type as unused,
simply use the type Void
Note:
Syntax “String…” indicates array of String values, similar to “String*+”
35
13. Android – Multi-Threading
Multi-Threading
AsyncTask's methods
onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is
normally used to setup the task, for instance by showing a progress bar in the user interface.
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The
result of the background computation is passed to this step as a parameter.
36
13. Android – Multi-Threading
Multi-Threading
Example: Using the AsyncTask class
The main task invokes an AsyncTask to do some slow job. The AsyncTask methods do the
required computation and periodically update the main’s UI. In our the example the
background activity negotiates the writing of the lines in the text box, and also controls the
circular progress bar.
37
13. Android – Multi-Threading
Multi-Threading
Example: Using the AsyncTask class
public class Main extends Activity {
Button btnSlowWork;
Button btnQuickWork;
EditText etMsg;
Long startingMillis;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etMsg = (EditText) findViewById(R.id.EditText01);
}// onCreate
38
13. Android – Multi-Threading
Multi-Threading
Example: Using the AsyncTask class
private class VerySlowTask extends AsyncTask <String, Long, Void> {
39
13. Android – Multi-Threading
Multi-Threading
Example: Using the AsyncTask class
// periodic updates - it is OK to change UI
@Override
protected void onProgressUpdate(Long... value) {
super.onProgressUpdate(value);
etMsg.append("\nworking..." + value[0]);
}
}//AsyncTask
}// Main
40
13. Android – Multi-Threading
Multi-Threading
Example: Using the AsyncTask class
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="7px" />
<Button
android:text="Do some SLOW work"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="7px" />
<Button
android:text="Do some QUICK work"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="7px" />
</LinearLayout>
41
13. Android – Multi-Threading
Multi-Threading
Questions
42