Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Implement an application that implements Multi threading

1)Open eclipse or android studio and select new android project
2)Give project name and select next
3) Choose the android version.Choose the lowest android version(Android 2.2) and select next
4) Enter the package name.package name must be two word seprated by comma and click finish
5)Go to package explorer in the left hand side.select our project.
6)Go to res folder and select layout.Double click the main.xml file.Add the code below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/info" >
      <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="fetchData"
        android:text="Start MULTITHREAD" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main thread" />
</LinearLayout>
7) Now select mainactivity.java file and type the following code.
Ypackage multi.threading;


//import your.first.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
public class MultiThreadingActivity extends Activity {
    private  TextView tvOutput;
    private  static final int t1 = 1;
    private  static final int t2 = 2;
    private  static final int t3 = 3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tvOutput = (TextView) findViewById(R.id.textView1);
    }
    public void fetchData(View v) {
        tvOutput.setText("Main thread");
        thread1.start();
        thread2.start();
        thread3.start();
    }
    Thread thread1 = new Thread(new Runnable() {
    @Override
    public void run() {
    for (int i = 0; i < 5; i++) {
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    handler.sendEmptyMessage(t1);
    }
    }
    });
    Thread thread2 = new Thread(new Runnable() {
      @Override
        public void run() {
        for (int i = 0; i < 5; i++) {
        try {
        Thread.sleep(1000);
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
        handler.sendEmptyMessage(t2);
        }
  
        }
        });
    Thread thread3 = new Thread(new Runnable() {
        @Override
        public void run() {
        for (int i = 0; i < 5; i++) {
        try {
        Thread.sleep(1000);
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
        handler.sendEmptyMessage(t3);
        }
             }
        });
       Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
    if(msg.what == t1) {
    tvOutput.append("\nIn thread 1");
    }
    if(msg.what == t2) {
        tvOutput.append("\nIn thread 2");
        }
    if(msg.what == t3) {
        tvOutput.append("\nIn thread 3");
        }
    }
    };
}
8)Now go to main.xml and right click .select run as option and select run configuration
9) Android output is present in the android emulator as shown in below.





3 comments:

Flag Counter