0% found this document useful (0 votes)
1 views

oopex8

The document outlines a Java program designed to implement multithreading concepts for managing and analyzing large datasets. It provides detailed steps for creating threads using both the Runnable interface and by extending the Thread class, along with example code for multiplication, addition, division, and summation operations. The program successfully demonstrates multithreading functionality with user input and verified output.

Uploaded by

isaacbenjamin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

oopex8

The document outlines a Java program designed to implement multithreading concepts for managing and analyzing large datasets. It provides detailed steps for creating threads using both the Runnable interface and by extending the Thread class, along with example code for multiplication, addition, division, and summation operations. The program successfully demonstrates multithreading functionality with user input and verified output.

Uploaded by

isaacbenjamin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

[Type here]

Ex. No. 8 MULTITHREADING

4/10/24 URK23AI1035

Aim :

To design and implement a comprehensive Java program that effectively utilizes


multithreading concepts to manage, process, and analyze large datasets containing record
information, thereby improving overall performance and efficiency.

Description :

Creating a Thread by Implementing the Runnable Interface

To create a thread by implementing the Runnable interface, follow these steps:

Step 1: Implement the run() Method

• The run() method is where the thread's execution logic is written.

Syntax:

public void run() {


// Thread logic here
}

Step 2: Instantiate a Thread Object

• You need to create a Thread object using a constructor that takes a Runnable object and a
thread name as parameters.

Syntax:

Thread(Runnable threadObj, String threadName);

Step 3: Start the Thread

• After creating the Thread object, you start the thread by calling the start() method,
which in turn calls the run() method.

Syntax:

threadObj.start();

Creating a Thread by Extending the Thread Class

To create a thread by extending the Thread class, follow these steps:


[Type here]

Step 1: Override the run() Method

• You override the run() method in your class to provide the logic that the thread will
execute.

Syntax:

public void run() {


// Thread logic here
}

Step 2: Instantiate the Thread and Start it

• Once you have created an object of your class, you can start the thread by calling the
start() method, which will execute the overridden run() method.

Syntax:

Thread threadObj = new YourThreadClass();


threadObj.start();

Program :

1.

class MultiplicationThread implements Runnable {

public void run() {

try {

for (int i = 1; i <= 10; i++) {

System.out.println("5 x " + i + " = " + (5 * i));

Thread.sleep(3000);

} catch (InterruptedException e) {

System.out.println("Thread interrupted.");
[Type here]

public static void main(String[] args) {

System.out.println("URK23AI1021--PIO MICHEAL");

MultiplicationThread table = new MultiplicationThread();

Thread thread = new Thread(table);

thread.start();

}OUTPUT:

2.

import java.util.Scanner;

class UserInputThread extends Thread {

public static int number1;

public static int number2;

public void run() {

Scanner scanner = new Scanner(System.in);

System.out.println("Thread t1: Enter two numbers:");


[Type here]

number1 = scanner.nextInt();

number2 = scanner.nextInt();

System.out.println("Input received: " + number1 + " and " + number2);

class AdditionThread extends Thread {

@Override

public void run() {

int sum = UserInputThread.number1 + UserInputThread.number2;

System.out.println("Thread t2: Addition result = " + sum);

class DivisionThread extends Thread {

@Override

public void run() {

if (UserInputThread.number2 != 0) {

double division = (double) UserInputThread.number1 / UserInputThread.number2;

System.out.println("Thread t3: Division result = " + division);

} else {

System.out.println("Thread t3: Division by zero is not allowed.");

class SumThread extends Thread {

@Override

public void run() {


[Type here]

int sum = 0;

for (int i = 1; i <= UserInputThread.number1; i++) {

sum += i;

System.out.println("Thread t4: Sum of numbers from 1 to " + UserInputThread.number1


+ " = " + sum);

public class MultiThreadingExample {

public static void main(String[] args)

{ System.out.println("URK23AI1021--PIO MICHEAL");

UserInputThread t1 = new UserInputThread();

AdditionThread t2 = new AdditionThread();

DivisionThread t3 = new DivisionThread();

SumThread t4 = new SumThread();

t1.setName("t1");

t2.setName("t2");

t3.setName("t3");

t4.setName("t4");

t1.start();

OUTPUT :
[Type here]

RESULT :

The program for multithreading has been completed and output is verified successfully

You might also like