---
title: "Android Multithreading Example"  
description: "A Thread is a concurrent unit of execution. It has its own call stack for methods being invoked, their arguments and local variables. Every applicatio"  
author: "Manoj Pandey"  
published: 2015-10-21  
updated: 2018-03-13  
canonical: https://www.mindstick.com/blog/10966/android-multithreading-example  
category: "android"  
tags: ["android", "thread"]  
reading_time: 3 minutes  

---

# Android Multithreading Example

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](https://www.mindstick.com/forum/34702/please-tell-me-what-is-cross-site-scripting-and-how-is-it-harmful-for-your-application) interacts with [components](https://www.mindstick.com/blog/74096/understanding-the-role-of-some-integral-ac-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](https://www.mindstick.com/articles/208957/a-complete-guide-for-android-app-development) 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](https://www.mindstick.com/articles/13015/5-practical-tips-and-techniques-to-write-an-essay) thread, AsyncTask etc.

Thread should be used in a long [running process](https://www.mindstick.com/articles/49/check-running-process-in-csharp-dot-net) 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](https://www.mindstick.com/articles/620/foreground-and-background-thread-in-c-sharp-threading) 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              public void 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();
```

**AsyncTask**enables proper and easy use of the UI thread. This class allows to perform background [operations](https://www.mindstick.com/blog/304985/how-does-devops-bridge-the-gap-between-development-and-operations-teams-like-git) 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](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp) 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(); 
```

---

Original Source: https://www.mindstick.com/blog/10966/android-multithreading-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
