0% found this document useful (0 votes)
51 views9 pages

Exno 7

The document describes an algorithm to create an Android application that implements multithreading. The steps are: 1. Create a new Android project in Android Studio and add a layout with buttons and textviews. 2. Add code to the activity class to define threads, handlers, and update the textview on thread completion. 3. The threads will run concurrently, sleeping for 1 second in each iteration and sending messages to the handler to update the textview with the thread name. 4. When the buttons are clicked, the threads are started and the textview will be updated every second to display the current thread name, demonstrating multithreading.

Uploaded by

Nikhitha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views9 pages

Exno 7

The document describes an algorithm to create an Android application that implements multithreading. The steps are: 1. Create a new Android project in Android Studio and add a layout with buttons and textviews. 2. Add code to the activity class to define threads, handlers, and update the textview on thread completion. 3. The threads will run concurrently, sleeping for 1 second in each iteration and sending messages to the handler to update the textview with the thread name. 4. When the buttons are clicked, the threads are started and the textview will be updated every second to display the current thread name, demonstrating multithreading.

Uploaded by

Nikhitha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Ex.

No:7
Implement an application that implements Multi threading

Aim:

To write the program to implement an application that


implements multithreading

Algorithm:

 Open eclipse or android studio and select new android project


 Give project name and select next
 Choose the android version.Choose the lowest android
version(Android 2.2) and select next
 Enter the package name.package name must be two word
seprated by comma and click finish
 Go to package explorer in the left hand side.select our project.
 Go to res folder and select layout.Double click the main.xml
file.Add the code below
 Now select mainactivity.java file and type the following code.
 Now go to main.xml and right click .select run as option and
select run configuration
 Android output is present in the android emulator as shown in
below
Code for main_Activity.xml:

<?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>

Code for main_Activity.java:


package 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"); }
}};}

Output:.
Result:
Thus the program to implement the multithreading was executed
successfully.

You might also like