PROGRAM4
PROGRAM4
Write a Java Program to implement the concept of multithreading with the use of
any three multiplication tables and assign three different priorities to them.
SOURCE CODE
import java.util.*;
public class PL4 extends Thread
{
private int tableNumber;
private int max;
public PL4(int tableNumber, int max)
{
this.tableNumber = tableNumber;
this.max = max;
}
public void run()
{
for (int i = 1; i <= max; i++)
{
System.out.println(tableNumber + " x " + i + " = " + (tableNumber * i));
try
{
Thread.sleep(1000); // Add a slight delay to better visualize the output
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
int limit = 5;
Scanner in =new Scanner(System.in);
System.out.println("Enter three numbers for multiplication tables: ");
int x=in.nextInt();
int y=in.nextInt();
int z=in.nextInt();
// Create three threads with different priorities
PL4 t1 = new PL4(x, limit);
PL4 t2 = new PL4(y, limit);
PL4 t3 = new PL4(z, limit);
// Set different priorities to the threads (MIN_PRIORITY = 1, MAX_PRIORITY = 10)
t1.setPriority(Thread.MIN_PRIORITY); // MIN=1
t2.setPriority(Thread.NORM_PRIORITY); //NORM=5
t3.setPriority(Thread.MAX_PRIORITY); //MAX=10
// Start the threads
t1.start();
t2.start();
t3.start();
}
}
PROGRAM-4-OUTPUT
Enter three numbers for multiplication tables:
5
8
10
8x1=8
5x1=5
10 x 1 = 10
8 x 2 = 16
5 x 2 = 10
10 x 2 = 20
8 x 3 = 24
10 x 3 = 30
5 x 3 = 15
5 x 4 = 20
10 x 4 = 40
8 x 4 = 32
10 x 5 = 50
5 x 5 = 25
8 x 5 = 40