75

I want some simple example on thread creation and invoking of threads in android.

mbejda
  • 1,471
  • 16
  • 25
ram
  • 3,487
  • 10
  • 33
  • 47

3 Answers3

89

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

Leandros
  • 16,805
  • 9
  • 69
  • 108
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
  • 10
    The 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
  • 5
    The 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
  • 1
    I'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
11

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:

Handler vs AsyncTask vs Thread

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Endian Ogino
  • 146
  • 3
9

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>
Paritosh
  • 2,097
  • 3
  • 30
  • 42
mbejda
  • 1,471
  • 16
  • 25
  • 51
    This 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
  • 1
    If 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
  • 2
    https://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
  • 1
    I'm pretty sure in recent versions of Android it would not even work sometimes like this. – Giszmo Apr 28 '15 at 04:01
  • 1
    It 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