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

Java 11,12 Exp

The document describes a Java program that implements a multi-threaded application with three threads. The program creates classes for a random number generator, square calculator, and cube calculator threads that continuously perform tasks. It starts the threads and they run concurrently calculating random numbers and their squares and cubes.

Uploaded by

shrihari.9919an
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)
25 views

Java 11,12 Exp

The document describes a Java program that implements a multi-threaded application with three threads. The program creates classes for a random number generator, square calculator, and cube calculator threads that continuously perform tasks. It starts the threads and they run concurrently calculating random numbers and their squares and cubes.

Uploaded by

shrihari.9919an
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/ 6

CS22409-JAVA PROGRAMMING: THEORY AND PRACTICES

DATE:
EX.NO:11

MULTI THREADED APPLICATION

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();
}
}

ROLL NO:2127220501140 Page | 35


class RandomNumberGenerator implements Runnable { private Random
random = new Random();
public void run()
{ while (true) {
int randomNumber = random.nextInt(100); System.out.println("Generated
random number: " + randomNumber); try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class SquareCalculator implements Runnable { private


RandomNumberGenerator generator;
public SquareCalculator(RandomNumberGenerator generator) { this.generator
= generator;
}
public void run() { while
(true) {
int randomNumber = generator.random.nextInt(100); if
(randomNumber % 2 == 0) {
int square = randomNumber * randomNumber; System.out.println("Square of " +
randomNumber + ": " + square);
}
}
}
}

class CubeCalculator implements Runnable { private


RandomNumberGenerator generator;
public CubeCalculator(RandomNumberGenerator generator)
{ this.generator = generator;
}
public void run() { while
(true) {
int randomNumber = generator.random.nextInt(100); if
(randomNumber % 2 != 0) {
int cube = randomNumber * randomNumber * randomNumber; System.out.println("Cube
of " + randomNumber + ": " + cube);
}
}
}
}

ROLL NO:2127220501140 Page | 36


SAMPLE INPUT AND OUTPUT:

RESULT:
Thus the Java program to implement a multi-threaded application that has three threads, has
been compiled and executed successfully.

ROLL NO:2127220501140 Page | 37


CS22409-JAVA PROGRAMMING: THEORY AND PRACTICES
DATE:
EX.NO:12

ALPHABET PRINTER USING THREADS

AIM:
To write a Java program to develop an application that executes two threads.

ALGORITHM:

1. Start the program.


2. Create a main class AlphabetPrinter to start the application.
3. Create two classes implementing the Runnable interface: ForwardAlphabetPrinter and
ReverseAlphabetPrinter.
4. In ForwardAlphabetPrinter class, use a loop to print alphabets from 'A' to 'Z' and sleep for
one second after each iteration.
5. In ReverseAlphabetPrinter class, use a loop to print alphabets from 'Z' to 'A' and sleep for
two seconds after each iteration.
6. In the main method, create instances of both threads and start them.
7. Use join() method to make sure the second thread waits for the first thread to finish execution
before starting.
8. Finally, print a message indicating that all threads have finished execution after both threads
complete their tasks.
9. End the program.

PROGRAM:

public class AlphabetPrinter {


public static void main(String[] args) {
Thread thread1 = new Thread(new ForwardAlphabetPrinter()); Thread
thread2 = new Thread(new ReverseAlphabetPrinter()); thread1.start();
try {
thread1.join();
}
catch (InterruptedException e) { e.printStackTrace();
}
thread2.start();

ROLL NO:2127220501140 Page | 38


try {
thread2.join();
}
catch (InterruptedException e) { e.printStackTrace();
}
System.out.println("All threads finished execution.");
}
}

class ForwardAlphabetPrinter implements Runnable { public void


run() {
for (char ch = 'A'; ch <= 'Z'; ch++) {
System.out.println(ch);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class ReverseAlphabetPrinter implements Runnable { public void


run() {
for (char ch = 'Z'; ch >= 'A'; ch--) {
System.out.println(ch);
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

ROLL NO:2127220501140 Page | 39


SAMPLE INPUT AND OUTPUT:

RESULT:
Thus the program to develop an application that executes two threads was compiled and
executed successfully.
ROLL NO:2127220501140 Page | 40

You might also like