multithreading in android

17
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android

Upload: pisces

Post on 23-Feb-2016

38 views

Category:

Documents


0 download

DESCRIPTION

Multithreading in Android. Alaa M. Alsalehi Software Engineer at IUG. Agenda. Multithreading vocabulary Main thread in android Time-consuming & blocking operations Unresponsive program Thread safety in android Message Passing is the solution Timer & Timer Task AsyncTask. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Multithreading in Android

ALAA M. ALSALEHISOFTWARE ENGINEER AT IUG

Multithreading in Android

Page 2: Multithreading in Android

Agenda

Multithreading vocabularyMain thread in androidTime-consuming & blocking operationsUnresponsive programThread safety in androidMessage Passing is the solutionTimer & Timer TaskAsyncTask

Page 3: Multithreading in Android

Multithreading vocabulary

Thread vs. processMultithreadingSynchronizationThread safeLock Design PatternMessage Passing PatternVolatileSwing multithreadingSwing utilitySwing worker

Page 4: Multithreading in Android

Main thread

All Android application components run on the main application thread Activities, Services, and Broadcast Receivers

Time-consumingBlocking operation In any component

will block all other components including Services and the visible Activity

Page 5: Multithreading in Android

Time-consuming & blocking operations

File operationsNetwork lookupsDatabase transactionsComplex calculationsetc

Page 6: Multithreading in Android

Unresponsive program

Android OS save himself from application does not response for input events.

Unresponsive programActivities

within 5 secondsBroadcast Receivers

onReceive handlers within 10 seconds.

Page 7: Multithreading in Android

Unresponsive Exception

Page 8: Multithreading in Android

Solution

Create your own threadprivate void doLongOperationInThread() { (new Thread(new Runnable() {

public void run() { String result = doLongOperation();

} })).start();

}

Page 9: Multithreading in Android

What about update UI from other thread?

private void doLongOperationInThread() { (new Thread(new Runnable() {

public void run() { String result = doLongOperation(); updateUI(result);

} })).start();

}

Page 10: Multithreading in Android

Thread safety in android

Page 11: Multithreading in Android

Exception

FATAL EXCEPTION: Thread-10 android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

at android.view.ViewRoot.checkThread(ViewRoot.java:2932) at android.view.ViewRoot.invalidateChild(ViewRoot.java:642) at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668) at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511) at android.view.View.invalidate(View.java:5279) at android.widget.TextView.checkForRelayout(TextView.java:5507) at android.widget.TextView.setText(TextView.java:2724) at android.widget.TextView.setText(TextView.java:2592) at android.widget.TextView.setText(TextView.java:2567) at

com.modonat.threadNeed.ThreadNeedActivity.updateUI(ThreadNeedActivity.java:46) at

com.modonat.threadNeed.ThreadNeedActivity.access$2(ThreadNeedActivity.java:44) at com.modonat.threadNeed.ThreadNeedActivity$2.run(ThreadNeedActivity.java:34) at java.lang.Thread.run(Thread.java:1019)

Page 12: Multithreading in Android

Is android UI thread-safe?

Unfortunately like swing the answer will be No.

Fortunately android has a good solution for this problem.

Main thread who has control on UI

Other thread who is doing

long/blocking operatiions

Create

Update UI

Page 13: Multithreading in Android

Message Passing

Android depend on message passing to solve this problem.

Page 14: Multithreading in Android

Handler

private void updateUIByHandler() { final Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { updateUI((String) msg.obj); }

}; (new Thread(new Runnable() {

public void run() { Message msg = myHandler.obtainMessage();//get message object

msg.obj = doLongOperation(1000);

myHandler.sendMessage(msg);//send message to handle it } })).start(); }

Page 15: Multithreading in Android

Timer

Like thread like timer in UI update.Timer timer=new Timer();timer.schedule(new TimerTask() {

@Override public void run() {

Message msg = myHandler.obtainMessage(); msg.obj = doLongOperation(1000); myHandler.sendMessage(msg);}

}, 1000);

Page 16: Multithreading in Android

AsyncTask

onPreExecute is invoked before the execution.

onPostExecute is invoked after the execution.

doInBackground  the main operation. Write your heavy operation here.

onProgressUpdate  Indication to the user on progress. It is invoked every

time publishProgress() is called.

Page 17: Multithreading in Android

Refrence

Android in Practice by CHARLIE COLLINS, MICHAEL D. GALPIN and MATTHIAS KÄPPLER

Professional Android™ Application Development by Reto Meier

Article “Android – Multithreading in a UI environment” http://www.aviyehuda.com/blog/2010/12/20/android-multithreading-in-a-ui-environment/