Write a Java program that implements a multi-thread application that has three threads. 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.
import java.util.Random;
public class MultiThreadNumberProcessor {
// Shared container class to hold the generated number and manage synchronization
static class NumberContainer {
private int number = 0;
private boolean available = false;
// Synchronized method for the Producer thread to set a new number
public synchronized void setNumber(int value) {
while (available) {
try {
// Wait if the previous number hasn't been processed yet
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
this.number = value;
this.available = true;
System.out.println("Producer generated: " + number);
// Notify waiting consumer threads that a new number is available
notifyAll();
}
// Synchronized method for consumer threads to get and process the number
public synchronized int getNumber() {
while (!available) {
try {
// Wait if no new number is available
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// Once retrieved, mark as unavailable for the next cycle
available = false;
// Notify the producer thread that it can generate the next number
notifyAll();
return number;
}
}
// Thread 1: Generates random integers every second
static class Producer implements Runnable {
private final NumberContainer container;
private final Random random = new Random();
public Producer(NumberContainer container) {
this.container = container;
}
@Override
public void run() {
while (true) {
int value = random.nextInt(100); // Generate a random integer between 0 and 99
container.setNumber(value);
try {
Thread.sleep(1000); // Wait for 1 second
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
}
// Thread 2: Computes and prints the square of even numbers
static class EvenProcessor implements Runnable {
private final NumberContainer container;
public EvenProcessor(NumberContainer container) {
this.container = container;
}
@Override
public void run() {
while (true) {
int number = container.getNumber();
if (number % 2 == 0) {
long square = (long) number * number;
System.out.println("Even Thread: " + number + "^2 = " + square);
}
}
}
}
// Thread 3: Computes and prints the cube of odd numbers
static class OddProcessor implements Runnable {
private final NumberContainer container;
public OddProcessor(NumberContainer container) {
this.container = container;
}
@Override
public void run() {
while (true) {
int number = container.getNumber();
if (number % 2 != 0) {
long cube = (long) number * number * number;
System.out.println("Odd Thread: " + number + "^3 = " + cube);
}
}
}
}
public static void main(String[] args) {
NumberContainer container = new NumberContainer();
Thread producerThread = new Thread(new Producer(container), "ProducerThread");
Thread evenThread = new Thread(new EvenProcessor(container), "EvenProcessorThread");
Thread oddThread = new Thread(new OddProcessor(container), "OddProcessorThread");
System.out.println("Starting threads...");
// Start all three threads
producerThread.start();
evenThread.start();
oddThread.start();
// Optional: Main thread can wait or handle program termination logic
try {
// Keep the program running indefinitely for demonstration
producerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment