Using Multithreading in Android

Regular Thread

Below is an example of a Runnable class, which runs the code in its Runnable.run() method on a separate thread.

(new Thread(new Runnable() {

          @Override
          public void run() {
                   doLongOperation();
          }
})).start();

Note:
– One or more Runnable objects that perform a particular operation are sometimes called a task.
Thread and Runnable are basic classes that, on their own, have only limited power.
Runnable won’t be running on the UI thread, so it can’t directly modify UI objects such as View objects.

Thread + Handler for UI operations

A new thread cannot update the user interface, so we can use a handler. The Handler is the middleman between a new thread and the UI message queue.

Example 1 – posting an object

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

(new Thread(new Runnable() {

          @Override
          public void run() {
                   Message msg = myHandler.obtainMessage();
                   msg.obj = doLongOperation();
                   myHandler.sendMessage(msg);
          }
})).start();

Example 2 – posting an Runnable

final Handler myHandler = new Handler();

          (new Thread(new Runnable() {

                   @Override
                   public void run() {
                             final String res = doLongOperation();
                             myHandler.post(new Runnable() {

                                      @Override
                                      public void run() {
                                                updateUI(res);
                                      }
                             });
                   }
          })).start();

}

Using AsyncTask

AsyncTask is a thread that can handle user interface operations.

class MyAsyncTask extends AsyncTask<integer, string,="" long=""> {

@Override
protected Long doInBackground(Integer... params) {

          long start = System.currentTimeMillis();
          for (Integer integer : params) {
                   publishProgress("start processing "+integer);
                   doLongOperation();
                   publishProgress("done processing "+integer);
          }

          return start - System.currentTimeMillis();
}

@Override
protected void onProgressUpdate(String... values) {
          updateUI(values[0]);
}

@Override
protected void onPostExecute(Long time) {
          updateUI("Done,it took:"+ time +"millisecondes");
}

@Override
protected void onPreExecute() {
          updateUI("Starting the process");
}


}

MyAsyncTask aTask = new MyAsyncTask();
aTask.execute(1, 2, 3, 4, 5);</integer,>

Note: AsyncTask defines 3 generic types:
AsyncTask<{type of the input}, {type of the update unit}, {type of the result}>.
You don’t have to use all of them – simply use Void for any of them.

Using a Timer to schedule jobs

Timer is a comfortable way to dispatch a thread in the future, be it once or more.

TimerTask timerTask = new TimerTask() {
          @Override
          public void run() {
                   doSomething();
          }
};

Timer timer = new Timer();
timer.schedule(timerTask, 2000,2000);
Software Engineer(Android) at Bennett Coleman and Co. Ltd. (Times Group)

This content has been helpful to you?

Thanks for contributing!

Yes No