0% found this document useful (0 votes)
19 views

Oops Exp No 9

Oops practical experiment 9

Uploaded by

anu303184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Oops Exp No 9

Oops practical experiment 9

Uploaded by

anu303184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EX NO :8 PROGRAM TO IMPLEMENT MULTITHREADED

APPLICATION

AIM: To write a java program that implements a multi-threaded application .

ALGORITHM:

STEP 1: Create a class even which implements first thread that computes .the square of the
number .
STEP 2: Initialise run() method implements the code to be executed when thread gets
executed.
STEP 3: Create a class odd which implements second thread that computes the cube of the
number.
STEP 4: Create a third thread that generates random number.If the random number is even , it
displays the square of the number.If the random number generated is odd , it displays the
cube of the given number .
STEP 5: The Multithreading is performed and the task switched between multiple threads.
STEP 6: The sleep () method makes the thread to suspend for the specified time.

PROGRAM:

MultiThreadRandOddEven:
javaimport java.util.*;
// class for Even Number
class EvenNum implements Runnable
{
public int a;
public EvenNum(int a) {
this.a = a;
}
public void run()
{
System.out.println("The Thread "+ a +" is EVEN and Square of " + a + " is : " + a * a);
}
}
// class for Odd Number
class OddNum implements Runnable {
public int a;
public OddNum(int a)
{
this.a = a;
}
public void run()
{
System.out.println("The Thread "+ a +" is ODD and Cube of " + a + " is: " + a * a * a);
}
}
// class to generate random number
class RandomNumGenerator extends Thread
{
public void run()
{
int n = 0;
Random rand = new Random();
try {
for (int i = 0; i < 10; i++)
{ n = rand.nextInt(20);
System.out.println("Generated Number is " + n);
// check if random number is even or odd
if (n % 2 == 0) {
Thread thread1 = new Thread(new EvenNum(n));
thread1.start();
}
else
{
Thread thread2 = new Thread(new OddNum(n));
thread2.start();
}
// thread wait for 1 second
Thread.sleep(1000);
System.out.println("------------------------------------");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
// Driver classpublic

class MultiThreadRandOddEven
{
public static void main(String[] args)
{
RandomNumGenerator rand_num = new RandomNumGenerator();
rand_num.start();
}
}

Output:
RESULT:
Thus the java application to perform Multithreaded Application was implemented and the
program was executed successfully.

You might also like