I want some simple example on thread creation and invoking of threads in android.
-
You can refer to this post too : https://stackoverflow.com/questions/9148899/returning-value-from-thread – Ravindra babu Aug 29 '17 at 10:27
3 Answers
This is a nice tutorial:
http://android-developers.blogspot.de/2009/05/painless-threading.html
Or this for the UI thread:
http://developer.android.com/guide/faq/commontasks.html#threading
Or here a very practical one:
http://www.androidacademy.com/1-tutorials/43-hands-on/115-threading-with-android-part1
and another one about procceses and threads
http://developer.android.com/guide/components/processes-and-threads.html

- 16,805
- 9
- 69
- 108

- 51,941
- 35
- 152
- 200
-
10The first two links are not working.. For the first link, try [link](http://android-developers.blogspot.com/2009/05/painless-threading.html) For the second link, try [link](http://developer.android.com/guide/components/processes-and-threads.html) – Dexter Jun 27 '12 at 14:57
-
5The answer is almost 1.5 year old. I try to update the links. – RoflcoptrException Jun 27 '12 at 18:34
-
you are really nice to have answered an unspecific question such as this – A Person Mar 29 '13 at 16:44
-
1I'd disagree that it is a non-specific question: it's asking the community for a very particular type of example, with a very particular focus. – WillC Nov 14 '16 at 22:07
One of Androids powerful feature is the AsyncTask class.
To work with it, you have to first extend it and override doInBackground
(...).
doInBackground
automatically executes on a worker thread, and you can add some
listeners on the UI Thread to get notified about status update, those functions are
called: onPreExecute()
, onPostExecute()
and onProgressUpdate()
You can find a example here.
Refer to below post for other alternatives:

- 37,698
- 11
- 250
- 211

- 146
- 3
-
-
-
2I've just updated the link. The blog has moved to this new location: [link](http://android-jotting.blogspot.com/2010/09/using-asynctasks-in-android.html) – slybloty Dec 09 '11 at 15:57
Here is a simple threading example for Android. It's very basic but it should help you to get a perspective.
Android code - Main.java
package test12.tt;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Test12Activity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView txt1 = (TextView) findViewById(R.id.sm);
new Thread(new Runnable() {
public void run(){
txt1.setText("Thread!!");
}
}).start();
}
}
Android application xml - main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id = "@+id/sm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
</LinearLayout>
-
51This seems to me like an incorrect example, since a UI update is being done from a background thread. – Frank Harper Feb 26 '12 at 17:01
-
1If you are updating ui thread you should use handler, because it's the middle layer between ui thread and your spawn thread , that way you will stay safe from dead locking – hackp0int Jun 24 '12 at 19:44
-
2https://developer.android.com/guide/components/processes-and-threads.html exactly discourages this kind of programming since it violates "Do not access the Android UI toolkit from outside the UI thread" rule – Serkan Arıkuşu Jan 15 '13 at 21:33
-
1I'm pretty sure in recent versions of Android it would not even work sometimes like this. – Giszmo Apr 28 '15 at 04:01
-
1It violates the second rule of the single-threaded model: do not access the Android UI toolkit from outside the UI thread – ThePatelGuy Mar 15 '16 at 03:56