Java program that implements a multi-thread application that hash tree threads. First thread generates a random integer for every 1 second; second thread computes the square of the number and prints; third thread will print the value of cube of the number.

				
					import java.util.*;
class second implements Runnable
{
public int x;
public second (int x)
{
this.x=x;
}
public void run()
{
System.out.println("Second thread:Square of the number
is"+x*x);
}
}
class third implements Runnable
{
public int x;
public third(int x)
{
this.x=x;
}
public void run()
{
System.out.println("third thread:Cube of the number is"+x*x*x);
}
}
class first extends Thread
{
public void run()
{
int num=0;
Random r=new Random();
try
{
for(int i=0;i<5;i++)
{
num=r.nextInt(100);
System.out.println("first thread generated number
is"+num);
Thread t2=new Thread (new second(num));
t2.start();
Thread t3=new Thread(new third(num));
t3.start();
Thread.sleep(1000);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());

}
}
}
public class multithread
{
public static void main(String args[])
{
first a=new first();
a.start();
}
}
				
			
OUTPUT:
first thread generated number is65
Second thread:Square of the number is4225
third thread:Cube of the number is274625
first thread generated number is33
Second thread:Square of the number is1089
third thread:Cube of the number is35937
first thread generated number is30
Second thread:Square of the number is900
third thread:Cube of the number is27000
first thread generated number is29
Second thread:Square of the number is841
third thread:Cube of the number is24389
first thread generated number is93
Second thread:Square of the number is8649
third thread:Cube of the number is804357

Leave a Reply