Skip to main content

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.

 CODE:


import java.util.Random;


class RandomNumberGenerator implements Runnable {

    public void run() {

        Random random = new Random();

        while (true) {

            try {

                Thread.sleep(1000); // Sleep for 1 second

                int number = random.nextInt(100); // Generate a random integer between 0 and 99

                System.out.println("Generated number: " + number);

                if (number % 2 == 0) {

                    Thread thread = new Thread(new SquareCalculator(number));

                    thread.start();

                } else {

                    Thread thread = new Thread(new CubePrinter(number));

                    thread.start();

                }

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

        }

    }

}


class SquareCalculator implements Runnable {

    private int number;


    public SquareCalculator(int number) {

        this.number = number;

    }


    public void run() {

        System.out.println("Square of " + number + " is: " + (number * number));

    }

}


class CubePrinter implements Runnable {

    private int number;


    public CubePrinter(int number) {

        this.number = number;

    }


    public void run() {

        System.out.println("Cube of " + number + " is: " + (number * number * number));

    }

}


public class MultiThreadExample {

    public static void main(String[] args) {

        Thread thread = new Thread(new RandomNumberGenerator());

        thread.start();

    }

}

Algorithm:
  1. Define a class RandomNumberGenerator implementing the Runnable interface.
  2. Inside RandomNumberGenerator class: a. Override the run() method. b. Create a Random object to generate random numbers. c. Inside an infinite loop: i. Sleep the thread for 1 second. ii. Generate a random integer between 0 and 99. iii. Print the generated number. iv. If the number is even, start a new thread for SquareCalculator passing the number as an argument. v. If the number is odd, start a new thread for CubePrinter passing the number as an argument.
  3. Define a class SquareCalculator implementing the Runnable interface.
  4. Inside SquareCalculator class: a. Define a private variable number. b. Define a constructor to initialize the number. c. Override the run() method. d. Print the square of the number.
  5. Define a class CubePrinter implementing the Runnable interface.
  6. Inside CubePrinter class: a. Define a private variable number. b. Define a constructor to initialize the number. c. Override the run() method. d. Print the cube of the number.
  7. Define a class MultiThreadExample.
  8. Inside MultiThreadExample class: a. Define the main method. b. Create a new thread for RandomNumberGenerator. c. Start the thread.
  9. End of the program.











Popular posts from this blog

krushnaaa

  import java . awt .* ; import java . awt . event .* ; public class SimpleAWTExample {     public static void main ( String [] args ) {         // Create a Frame         Frame frame = new Frame ( "Simple AWT Example" );         // Create a Button         Button button = new Button ( "Click Me!" );         // Set layout for the Frame         frame . setLayout ( new FlowLayout ());         // Add the Button to the Frame         frame . add ( button );         // Set size of the Frame         frame . setSize ( 300 , 200 ); // Width: 300 pixels, Height: 200 pixels         // Make the Frame visible         frame . setVisible ( true );         // Add a WindowListener to handle closing event     ...

Write a program to create a class Student2 along with two method getData (), printData () to get the value through argument and display the data in printData. Create the two objects s1, s2 to declare and access the values from class STtest.

 CODE: import java.util.Scanner; class Student2 {     private String name;     private int age;          // Method to set data     public void getData(String name, int age) {         this.name = name;         this.age = age;     }          // Method to print data     public void printData() {         System.out.println("Name: " + name);         System.out.println("Age: " + age);     } } public class STtest {     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);                  // Creating objects of Student2 class         Student2 s1 = new Student2();         Student2 s2 = new Student2();              ...

Create an in-order threaded binary search tree and perform the traversals.(Double Thread)

 CODE: #include <iostream> using namespace std; struct Node {     int data;     Node* left;     Node* right;     bool leftThread;     bool rightThread; }; Node* createThreadedBST(int arr[], int n) {     Node* root = nullptr;     for (int i = 0; i < n; ++i) {         Node* newNode = new Node;         newNode->data = arr[i];         newNode->left = newNode->right = nullptr;         newNode->leftThread = newNode->rightThread = true;         if (root == nullptr) {             root = newNode;         } else {             Node* current = root;             Node* parent = nullptr;             while (true) {                ...