1 / 17

Multithreading in Android

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.

pisces
Download Presentation

Multithreading in Android

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multithreading in Android Alaa M. Alsalehi Software Engineer at IUG

  2. 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

  3. Multithreading vocabulary Thread vs. process Multithreading Synchronization Thread safe Lock Design Pattern Message Passing Pattern Volatile Swing multithreading Swing utility Swing worker

  4. 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

  5. Time-consuming & blocking operations File operations Network lookups Database transactions Complex calculations etc

  6. 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.

  7. Unresponsive Exception

  8. Solution • Create your own thread • privatevoiddoLongOperationInThread() { • (new Thread(new Runnable() { • publicvoid run() { • String result = doLongOperation(); • } • })).start(); • }

  9. What about update UI from other thread? • privatevoiddoLongOperationInThread() { • (new Thread(new Runnable() { • publicvoid run() { • String result = doLongOperation(); • updateUI(result); • } • })).start(); • }

  10. Thread safety in android

  11. 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)

  12. 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

  13. Message Passing Android depend on message passing to solve this problem.

  14. 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(); }

  15. 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);

  16. 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.

  17. 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/

More Related