300 likes | 571 Views
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.
E N D
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
Multithreading vocabulary Thread vs. process Multithreading Synchronization Thread safe Lock Design Pattern Message Passing Pattern Volatile Swing multithreading Swing utility Swing worker
Main thread • All Android application components run on the main application thread • Activities, Services, and Broadcast Receivers • Time-consuming • Blocking operation • In any component • will block all other components including Services and the visible Activity
Time-consuming & blocking operations File operations Network lookups Database transactions Complex calculations etc
Unresponsive program • Android OS save himself from application does not response for input events. • Unresponsive program • Activities • within 5 seconds • Broadcast Receivers • onReceive handlers within 10 seconds.
Solution • Create your own thread • privatevoiddoLongOperationInThread() { • (new Thread(new Runnable() { • publicvoid run() { • String result = doLongOperation(); • } • })).start(); • }
What about update UI from other thread? • privatevoiddoLongOperationInThread() { • (new Thread(new Runnable() { • publicvoid run() { • String result = doLongOperation(); • updateUI(result); • } • })).start(); • }
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)
Is android UI thread-safe? Unfortunately like swing the answer will be No. Fortunately android has a good solution for this problem. Create Other thread who is doing long/blocking operatiions Main thread who has control on UI Update UI
Message Passing Android depend on message passing to solve this problem.
Handler privatevoidupdateUIByHandler() { final Handler myHandler = new Handler() { @Override publicvoidhandleMessage(Message msg) { updateUI((String) msg.obj); } }; (new Thread(new Runnable() { publicvoid run() { Message msg = myHandler.obtainMessage();//get message object msg.obj = doLongOperation(1000); myHandler.sendMessage(msg);//send message to handle it } })).start(); }
Timer • Like thread like timer in UI update. • Timer timer=new Timer(); • timer.schedule(newTimerTask() { • @Override • publicvoid run() { • Message msg = myHandler.obtainMessage(); • msg.obj = doLongOperation(1000); • myHandler.sendMessage(msg);} • }, 1000);
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.
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/