Java 11,12 Exp
Java 11,12 Exp
DATE:
EX.NO:11
AIM:
To develop a Java program to implement a multi-threaded application that has
three threads
ALGORITHM:
1. Start the program.
2. Create a main class MultiThreadedApp to start the application.
3. Create three classes implementing the Runnable interface:
RandomNumberGenerator, SquareCalculator, and CubeCalculator.
4. In RandomNumberGenerator class, continuously generate a random integer between 0
and 99 every second and print it.
5. In SquareCalculator class, receive the random number generated by
RandomNumberGenerator, compute the square if it's even, and print the result.
6. In CubeCalculator class, receive the random number generated by
RandomNumberGenerator, compute the cube if it's odd, and print the result.
7. In the main method, create instances of the threads and start them.
8. Each thread continuously performs its task as per the described conditions.
9. End the program.
PROGRAM:
import java.util.Random;
public class MultiThreadedApp {
public static void main(String[] args) {
RandomNumberGenerator generator = new RandomNumberGenerator(); Thread thread1
= new Thread(generator);
SquareCalculator squareCalculator = new SquareCalculator(generator); Thread thread2 =
new Thread(squareCalculator);
CubeCalculator cubeCalculator = new CubeCalculator(generator); Thread thread3 =
new Thread(cubeCalculator);
thread1.start();
thread2.start();
thread3.start();
}
}
RESULT:
Thus the Java program to implement a multi-threaded application that has three threads, has
been compiled and executed successfully.
AIM:
To write a Java program to develop an application that executes two threads.
ALGORITHM:
PROGRAM:
RESULT:
Thus the program to develop an application that executes two threads was compiled and
executed successfully.
ROLL NO:2127220501140 Page | 40