0% found this document useful (0 votes)
12 views5 pages

Az Oop 8 PDF

The document outlines a lab exercise on multithreading in Java, focusing on extending the Thread class to solve specific problems. It includes example programs for creating a multiplication table thread and a multi-threaded application that performs addition, division, and summation based on user input. The exercise demonstrates the benefits of multithreading for executing multiple tasks concurrently within a single program.

Uploaded by

rohith07b
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)
12 views5 pages

Az Oop 8 PDF

The document outlines a lab exercise on multithreading in Java, focusing on extending the Thread class to solve specific problems. It includes example programs for creating a multiplication table thread and a multi-threaded application that performs addition, division, and summation based on user input. The exercise demonstrates the benefits of multithreading for executing multiple tasks concurrently within a single program.

Uploaded by

rohith07b
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/ 5

23DC2002 - Object Oriented Programming Lab Reg.No.

URK23CO2018

Ex. No. 8 Multithreading

Date of Exercise

Aim

To use the concepts of Multithreading by extending the Thread class for the given problems using
Java language.

Description

Java multithreading allows multiple threads to run concurrently, making it possible to execute
multiple tasks simultaneously within a single program. It can improve the performance of
applications

Multithreading Syntax:

Creating a thread:

class MyThread extends Thread {

public void run() {

System.out.println("Thread is running...");

}}

public class Main {

public static void main(String[] args) {

MyThread thread = new MyThread();

thread.start(); } }

1
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

1. Write a program that create a thread and display the line of multiplication table of
number 5 for every 3 seconds.

Program

class MultiplicationTableThread extends Thread {


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 was interrupted.");
}
}
}

public class MultiplicationTable {


public static void main(String[] args) {
System.out.println("URK23CO2018");
MultiplicationTableThread thread = new MultiplicationTableThread();
thread.start();
}
}
Output Screenshot

2
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

2. Create a java application that uses multi-threading concept to perform the following.

1. Getting inputs from user by using the thread name called“t1”.

2. Perform addition operation using the thread name called “t2”

3. Perform division operation using the thread name called “t3”

4. Perform Sum of numbers using the thread name called “t4” Program

import java.util.Scanner;

class InputThread extends Thread {


int num1, num2;

public InputThread() {
super("t1");
}

public void run() {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
num2 = scanner.nextInt();
}
}

class AdditionThread extends Thread {


int num1, num2;

public AdditionThread(int num1, int num2) {


super("t2");
this.num1 = num1;
this.num2 = num2;
}

3
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

public void run() {


int sum = num1 + num2;
System.out.println("Addition result by " + Thread.currentThread().getName() + ": " + sum);
}
}

class DivisionThread extends Thread {


int num1, num2;

public DivisionThread(int num1, int num2) {


super("t3");
this.num1 = num1;
this.num2 = num2;
}

public void run() {


if (num2 != 0) {
double result = (double) num1 / num2;
System.out.println("Division result by " + Thread.currentThread().getName() + ": " + result);
} else {
System.out.println("Division by zero is not allowed.");
}
}
}

class SumThread extends Thread {


int num1, num2;

public SumThread(int num1, int num2) {


super("t4");
this.num1 = num1;
this.num2 = num2;
}

public void run() {


int sum = 0;
for (int i = num1; i <= num2; i++) {

4
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

sum += i;
}
System.out.println("Sum of numbers between " + num1 + " and " + num2 + " by " +
Thread.currentThread().getName() + ": " + sum);
}
}

public class MultiThreadingApplication {


public static void main(String[] args) throws InterruptedException {
System.out.println("URK23CO2018");
InputThread t1 = new InputThread();
t1.start();
t1.join();
AdditionThread t2 = new AdditionThread(t1.num1, t1.num2);
DivisionThread t3 = new DivisionThread(t1.num1, t1.num2);
SumThread t4 = new SumThread(t1.num1, t1.num2);

t2.start();
t3.start();
t4.start();

t2.join();
t3.join();
t4.join();}
}
Output Screenshot

Result

Theus the implementation of multithreading Program has been executed successfully.

You might also like