Java multi threading program
import java.util.Scanner;
class Thread1 extends Thread
{
public void run()
{
Scanner s = new Scanner(System.in);
int n;
n=s.nextInt();
for(int i=1; i <= 10; i++)
{
System.out.println(n+" * "+i+" = "+n*i);
}
}
}
class Thread2 extends Thread
{
public void run()
{
float fact=1; int n;
Scanner sc=new Scanner(System.in);
n =sc.nextInt();
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("fact="+fact);
}
}
class Thread3 extends Thread
{
public void run()
{
int num;
Scanner input = new Scanner(System.in);
num = input.nextInt();
if (10 % 2 != 0 )
System.out.println(num+" is an even number.");
else
System.out.println(num+" is an odd number.");
}
}
public class MultithreadingExpt
{
public static void main(String[] args)
{
System.out.println("Main thread started");
Thread1 obj1 = new Thread1();
Thread2 obj2 = new Thread2();
Thread3 obj3 = new Thread3();
obj3.setPriority(Thread.MAX_PRIORITY); // priority = 10
obj2.setPriority(Thread.NORM_PRIORITY); // priority = 6
obj1.setPriority(Thread.MIN_PRIORITY); // priority =l
obj1.start();
obj2.start();
obj3.start();
}
}
Comments
Post a Comment