blog

Home / DeveloperSection / Blogs / Android Multithreading Example

Android Multithreading Example

Manoj Pandey3608 21-Oct-2015

A Thread is a concurrent unit of execution. It has its own call stack for methods being invoked, their arguments and local variables. Every application has at least one thread when application start and call it main thread.  It is also the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread.

By default android app run on main thread. For optimize ANR (Application Not Responding) use android ui thread. See the below example. Basically ui thread is used to reflect ui operation.

runOnUiThread(new Runnable() {

             

              @Override

              public void run() {

                   // TODO Auto-generated method stub

                   // Update ui

                  

              }

          });

 

Basically thread use for to execute long process and execute code parallel.  For execute long process in android you can use different techniques thread, AsyncTask etc.

Thread should be used in a long running process that would block the UI from updating. If it's more than a second or two you might want to put it into a background thread and notify the user with a dialog or spinner or something. If you lock the UI thread for more than 5 seconds the user will be prompted with a kill or wait option by the OS.

new Thread(new Runnable() {

             

              @Override

              publicvoid run() {

                   // TODO Auto-generated method stub

                  

              }

          }).start();

 

If you are using thread it can be problem to ui operation. You can use Handler to execute ui operation. OR

 

new Thread(new Runnable() {

             

              @Override

              public void run() {

                   // TODO Auto-generated method stub

                   ChatActivity.this.runOnUiThread(new Runnable() {

 

              @Override

              public void run() {

              // you can update ui on this thread

 

              }

          });

 

              }

          }).start();

 

AsyncTaskenables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. In AsyncTask does not block UI if we execute code on Background. For example we are doing network process and it can be take long time to perform operation then it will be freeze UI, so we use AsyncTask to handle this operation.

AsyncTask has four steps:

doInBackground: Code performing long running operation goes in this method.  When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed.

onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.

onPreExecute: This method is called before doInBackground method is called.

onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method.

 
Example of AsyncTask

class AsycInfo extends AsyncTask<Void, Void, Void> {

          Message message;

 

          @Override

          protected void onPreExecute() {

              super.onPreExecute();

 

          }

 

          @Override

          protected Void doInBackground(Void... params) {     

                  

             // perform long running operation

             

              return null;

          }

 

          @Override

          protected void onPostExecute(Void args) {

              super.onPostExecute(args);

 

         // after perform long running operation update ui

 

          }

     }

 // Call AsyncMethod

new AsycInfo().execute();

 


Updated 13-Mar-2018

Leave Comment

Comments

Liked By