The document describes a Java program that uses multithreading to generate random numbers and classify them as even or odd in separate threads. The program creates Even and Odd thread classes that print the number, if it is even or odd, and its square or cube. A Demo thread generates 5 random numbers, checks if they are even or odd, and starts the corresponding Even or Odd thread to process the number. The main method starts the Demo thread to run the program.
The document describes a Java program that uses multithreading to generate random numbers and classify them as even or odd in separate threads. The program creates Even and Odd thread classes that print the number, if it is even or odd, and its square or cube. A Demo thread generates 5 random numbers, checks if they are even or odd, and starts the corresponding Even or Odd thread to process the number. The main method starts the Demo thread to run the program.
The document describes a Java program that uses multithreading to generate random numbers and classify them as even or odd in separate threads. The program creates Even and Odd thread classes that print the number, if it is even or odd, and its square or cube. A Demo thread generates 5 random numbers, checks if they are even or odd, and starts the corresponding Even or Odd thread to process the number. The main method starts the Demo thread to run the program.
public int x; public even(int x) { this.x = x; } public void run() { System.out.println("Number "+ x +" is EVEN"); System.out.println("Square of " + x + " is : " + x*x); } }
class odd extends Thread {
public int x; public odd(int x) { this.x = x; } public void run() { System.out.println("Number "+ x +" is ODD"); System.out.println("Cube of " + x + " is : " + x*x*x); } }
class demo extends Thread {
public void run() { int num = 0; Random r = new Random(); try { for (int i = 0; i < 5; i++) { num = r.nextInt(100); System.out.println("Random Number is : " + num); if (num % 2 == 0) { Thread t1 = new Thread(new even(num)); t1.start(); } else { Thread t2 = new Thread(new odd(num)); t2.start(); } Thread.sleep(1000); System.out.println("\n"); } } catch (Exception ex) { System.out.println(ex.getMessage()); } } }
public class multithread {
public static void main(String[] args) { Thread t3 = new Thread(new demo()); t3.start(); } } OUTPUT:-