Computer Science, asked by kusumanjali1804, 6 months ago

Write a java program that implements a multi-threaded application
that has three threads. First thread generates a random integer
every 1 second and if the value is even, second thread computes
the square root of the number and prints. If the value is odd, the
third thread will print the value of square of the number.

Answers

Answered by ashokkumarchaurasia
7

Explanation:

class Square extends Thread

{

int x;

Square(int n)

{

x = n;

}

public void run()

{

int sqr = x * x;

System.out.println("Square of " + x + " = " + sqr );

}

}

class Cube extends Thread

{

int x;

Cube(int n)

{

Answered by akhulbay2
7

Answer:

import java.util.Random;

class Square extends Thread

{

 int x;

 Square(int n)

 {

 x = n;

 }

 public void run()

 {

 int sqr = x * x;

 System.out.println("Square of " + x + " = " + sqr );

 }

}

class Cube extends Thread

{

 int x;

 Cube(int n)

 {x = n;

 }

 public void run()

 {

 int cub = x * x * x;

 System.out.println("Cube of " + x + " = " + cub );

 }

}

class Number extends Thread

{

 public void run()

 {

 Random random = new Random();

 for(int i =0; i<5; i++)

 {

 int randomInteger = random.nextInt(100);

 System.out.println("Random Integer generated : " + randomInteger);

 Square s = new Square(randomInteger);

 s.start();

 Cube c = new Cube(randomInteger);

 c.start();

 try {

 Thread.sleep(1000);

} catch (InterruptedException ex) {

 System.out.println(ex);

}

 }

 }

}

public class Thr {

 public static void main(String args[])

 {

 Number n = new Number();

 n.start();

 }

}

please mark as brainlist

Similar questions