0% found this document useful (0 votes)
3 views8 pages

Experiment-9 OOPS

The document presents an experiment aimed at demonstrating multi-threading in Java using both inheritance and interfaces. It includes two programs: one that uses the Thread class to print numbers and letters, and another that implements the Runnable interface for the same purpose. The main thread waits for both threads to finish before concluding the execution.

Uploaded by

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

Experiment-9 OOPS

The document presents an experiment aimed at demonstrating multi-threading in Java using both inheritance and interfaces. It includes two programs: one that uses the Thread class to print numbers and letters, and another that implements the Runnable interface for the same purpose. The main thread waits for both threads to finish before concluding the execution.

Uploaded by

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

IIOT RAGHAV JINDAL 00417711723

EXPERIMENT-9
Aim: Design a program to demonstrate multi-threading using Thread Class.
Theory:
IIOT RAGHAV JINDAL 00417711723

Program:
1. Using Inheritance:
// Class that demonstrates multi-threading
public class MultiThreadExample {
// Thread class for printing numbers
static class PrintNumbersThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
// Thread class for printing letters
static class PrintLettersThread extends Thread {
public void run() {
for (char c = 'A'; c <= 'E'; c++) {
System.out.println("Letter: " + c);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(e);
}
IIOT RAGHAV JINDAL 00417711723

}
}
}

public static void main(String[] args) {


// Create two thread objects
PrintNumbersThread numbersThread = new PrintNumbersThread();
PrintLettersThread lettersThread = new PrintLettersThread();

// Start the threads


numbersThread.start(); // Start thread for printing numbers
lettersThread.start(); // Start thread for printing letters

try {
// Wait for both threads to finish
numbersThread.join();
lettersThread.join();
} catch (InterruptedException e) {
System.out.println(e);
}

System.out.println("Main thread finished!");


}
}

2. Using Interfaces
// Class that demonstrates multi-threading using Runnable interface
public class MultiThreadUsingRunnable {
// Runnable class for printing numbers
IIOT RAGHAV JINDAL 00417711723

static class PrintNumbersRunnable implements Runnable {


public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Number: " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}

// Runnable class for printing letters


static class PrintLettersRunnable implements Runnable {
public void run() {
for (char c = 'A'; c <= 'E'; c++) {
System.out.println("Letter: " + c);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}

public static void main(String[] args) {


IIOT RAGHAV JINDAL 00417711723

// Create runnable objects


Runnable numbersRunnable = new PrintNumbersRunnable();
Runnable lettersRunnable = new PrintLettersRunnable();

// Create thread objects and pass the Runnable objects to them


Thread numbersThread = new Thread(numbersRunnable);
Thread lettersThread = new Thread(lettersRunnable);

// Start the threads


numbersThread.start(); // Start thread for printing numbers
lettersThread.start(); // Start thread for printing letters

try {
// Wait for both threads to finish
numbersThread.join();
lettersThread.join();
} catch (InterruptedException e) {
System.out.println(e);
}

System.out.println("Main thread finished!");


}
}
IIOT RAGHAV JINDAL 00417711723

1. Using Inheritance:
IIOT RAGHAV JINDAL 00417711723

2. Using Interfaces
IIOT RAGHAV JINDAL 00417711723

Output:

Learning Outcome:

You might also like