0% found this document useful (0 votes)
2 views2 pages

Multi-Threaded Random Number Processing

The document describes a Java program that implements a multi-threaded application with three threads. The first thread generates a random integer every second, while the second thread computes and prints the square of the number if it is even, and the third thread prints the cube if the number is odd. The code includes classes for number generation, square computation, and cube computation, along with a main class to initiate the process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Multi-Threaded Random Number Processing

The document describes a Java program that implements a multi-threaded application with three threads. The first thread generates a random integer every second, while the second thread computes and prints the square of the number if it is even, and the third thread prints the cube if the number is odd. The code includes classes for number generation, square computation, and cube computation, along with a main class to initiate the process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program-2

Write a Java program that implements a multi-thread application


that has three threads. The first thread generates a random integer
every 1 second and if the value is even, the second thread computes
the square of the number and prints. If the value is odd, the third
thread will print the value of the cube of the number.

Code :

import [Link].*;

//Random Number Generation

class NumberGenerator extends Thread {


public void run() {
Random rand = new Random();
while (true) {
int number = [Link](100); // Generates number from 0 to 99
[Link]("\nGenerated Number: " + number);

if (number % 2 == 0) {
new SquareThread(number).start();
} else {
new CubeThread(number).start();
}

try {
[Link](1000); // wait 1 second
} catch (InterruptedException e) {
[Link](e);
}
}
}
}

// Squre Thread

class SquareThread extends Thread {


int number;

SquareThread(int number) {
[Link] = number;
}

public void run() {


int square = number * number;
[Link]("Square of " + number + " is " + square);
}
}

//Cube Thread
class CubeThread extends Thread {
int number;

CubeThread(int number) {
[Link] = number;
}

public void run() {


int cube = number * number * number;
[Link]("Cube of " + number + " is " + cube);
}
}

//Main Class
public class MultiThread {
public static void main(String[] args) {
NumberGenerator generator = new NumberGenerator();
[Link](); // Start generating numbers
}
}

Common questions

Powered by AI

If the generated random integer is 5, the sequence would involve the creation of a 'CubeThread', since 5 is odd. The 'CubeThread' will execute its 'run' method, which calculates 5 * 5 * 5, outputting: "Cube of 5 is 125". There would be no output from the 'SquareThread' as it is not used in this case .

The program utilizes the 'Thread.sleep(1000)' method within the 'NumberGenerator' thread to pause execution for one second before generating a new number, ensuring consistent intervals between number generations. This mechanism allows the program to wait and space out number production uniformly over time .

Under high load, the program could suffer from performance degradation due to the excessive creation and destruction of threads every second. This might lead to increased CPU and memory usage, slowing down computations. A solution would be to implement a thread pool, allowing threads to be reused rather than continually being created and destroyed, enhancing throughput and reducing latency .

The 'interrupt handling' mechanism in the 'NumberGenerator' thread is used to manage the sleep state interruption. The thread calls 'Thread.sleep(1000)' to pause execution for one second between number generation cycles. If the thread is interrupted while sleeping, it catches the 'InterruptedException' and outputs the exception message, ensuring that the thread can cleanly handle interruptions without crashing .

The program determines whether to compute the square or the cube of the generated number by checking the parity of the number. If the generated number is even (i.e., number % 2 == 0), it instantiates and starts a new 'SquareThread' to compute the square of the number. If the number is odd, it instantiates and starts a 'CubeThread' to calculate the cube of the number .

The main class 'MultiThread' is responsible for initiating the application by starting the 'NumberGenerator' thread, which manages the core logic of random number generation and triggering other threads. In contrast, the other classes, 'SquareThread' and 'CubeThread', are specialized thread classes dedicated to performing specific arithmetic operations (square and cube) based on the even or odd nature of the generated number. This separation allows distinct responsibilities across different classes .

To improve efficiency and scalability, the design could be modified to use a ThreadPoolExecutor to manage thread resources more efficiently. Instead of creating a new thread for each computation, a pool of threads can be maintained and reused, thus reducing the overhead of thread creation and destruction. Additionally, introducing a queue to store generated numbers and dispatch them to a limited number of worker threads can better manage workloads and improve performance under high-load conditions .

The 'NumberGenerator' thread is responsible for generating a random integer every second. It checks whether the integer is even or odd and consequently instantiates either the 'SquareThread' to compute the square of the number (if even) or 'CubeThread' to compute the cube (if odd) and starts these threads to carry out the respective computations .

Creating new threads each time a number is generated in the Java multi-thread program may lead to high memory and processing overhead, especially as the frequency of generating numbers is every second. Continuous thread creation and destruction can consume significant resources, potentially affecting program performance. In a high-frequency scenario, thread pooling might be considered to mitigate this overhead, allowing thread reuse rather than constant creation and destruction .

The Java program does not explicitly address thread synchronization concerns as each thread computation (SquareThread or CubeThread) is isolated from one another; they are executed in parallel without modifying shared data structures. Each computation is independent and state-specific, which mitigates the need for explicit synchronization mechanisms. However, if there were shared resources being modified, synchronization would need to be addressed to avoid race conditions .

You might also like