In Java Multithreading we can create multiple threads to run
In Java Multithreading we can create multiple threads to run different tasks. Write a multithreaded Java program that creates and runs three threads:
The main thread randomly generates 10 integer numbers between [1 100].
The first thread prints the average number of the 10 numbers
The second thread prints the maximum number of the 10 numbers.
The third thread prints the minimum number of the 10 numbers.
In the main thread, before it is start, print out “Now we start to print out!”, then until all three threads finish, print out “All threads are finished!”
Solution
MainClass.java
import java.util.Random;
public class MainClass {
public static void main(String[] args) {
//Creating the Object for GetMinMax class
GetMinMax gmm=new GetMinMax();
//calling the method on GetMinMax object which will generate 10 random numbers
gmm.generateRandomNos();
//Creating First Thread
MyThread1 t1=new MyThread1(gmm);
//Creating Second Thread
MyThread2 t2=new MyThread2(gmm);
//Creating Third Thread
MyThread3 t3=new MyThread3(gmm);
System.out.println(\"_______Now we start to print out______\");
//Setting the names to the threads
t1.setName(\"Thread#1\");
t2.setName(\"Thread#2\");
t3.setName(\"Thread#3\");
//Starting the thread 1
t1.start();
//Starting the thread 2
t2.start();
//Starting the thread 3
t3.start();
try {
//Here we are making main class sleep for 1500 milli seconds.
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(\"_______All threads are finished!______\");
}
}
__________________________________________
GetMinMax.java
import java.util.Random;
public class GetMinMax {
static int randomNos[];
//This method will generate the random numbers between 1 and 100
public static void generateRandomNos()
{
//Creating an integer array of size 10.
randomNos = new int[10];
//Creating an Random class object.
Random rand=new Random();
/* This loop will generates the random numbers between 1 and 100
* and populating those random numbers into an array.
*/
for(int i=0;i<10;i++)
{
//Generating a random number.
randomNos[i]=rand.nextInt((100 - 1) + 1) + 1;
}
}
public static double calcAvg()
{
//Declaring variable
int sum=0;
double average=0.0;
/* This method will calculate the sum
* of all randomly generated numbers
*/
for(int i=0;i<10;i++)
{
//calculating the sum
sum+=randomNos[i];
}
//calculating the average
average=sum/10;
return average;
}
//This method will calculate the maximum of 10 random numbers
public static int findMax()
{
//assigning the first element of randomNos[] to the variable max
int max=randomNos[0];
//This loop will finds the maximum element in the array
for(int i=0;i<10;i++)
{
if(randomNos[i]>max)
max=randomNos[i];
}
return max;
}
//This method will calculate the minimum of 10 random numbers
public static int findMin()
{
//assigning the first element of randomNos[] to the variable min
int min=randomNos[0];
//This loop will finds the minimum element in the array
for(int i=0;i<10;i++)
{
if(randomNos[i]<min)
min=randomNos[i];
}
return min;
}
}
_________________________________________________
MyThread1.java
public class MyThread1 extends Thread {
GetMinMax g;
//parameterized constructor
public MyThread1(GetMinMax g) {
this.g = g;
}
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name+\" Calculates the Average of 10 random Numbers is :\"+g.calcAvg());
}
}
___________________________________________
MyThread2.java
public class MyThread2 extends Thread {
GetMinMax g;
public MyThread2(GetMinMax g) {
this.g = g;
}
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name+\" finds the Maximum of 10 numbers is :\"+g.findMax());
}
}
_____________________________________________
MyThread3.java
public class MyThread3 extends Thread {
GetMinMax g;
public MyThread3(GetMinMax g) {
this.g = g;
}
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name+\" finds the Minimum of 10 numbers is :\"+g.findMin());
}
}
_________________________________________
Output:
_______Now we start to print out______
Thread#3 finds the Minimum of 10 numbers is :28
Thread#2 finds the Maximum of 10 numbers is :100
Thread#1 Calculates the Average of 10 random Numbers is :61.0
_______All threads are finished!______
___________________________________Thank You



