Program for creating three threads

In this program, we have created three threads X, Y and Z. Each of them will print message for 3 times as shown in output.


PROGRAM
class X extends Thread {
 
 public void run()  {
  
  for(int i=1;i<=3;i++) {
   System.out.println("From Thread X : "+i);
  }
  System.out.println("Exit Thread X");
 }
}

class Y extends Thread {
 
 public void run() {
  
  for(int i=1;i<=3;i++) {
   System.out.println("From Thread Y : "+i);
  }
  System.out.println("Exit Thread Y");
 }
}

class Z extends Thread {
 
 public void run()  {
  
  for(int i=1;i<=3;i++) {
   System.out.println("From Thread Z : "+i);
  }
  System.out.println("Exit Thread Z");
 }
}

public class ThreeThreads {

 public static void main(String[] args) {
 
  new X().start();
  new Y().start();
  new Z().start();
 }

}
OUTPUT
C:\>javac ThreeThreads.java
C:\>java ThreeThreads
From Thread X : 1
From Thread X : 2
From Thread X : 3
Exit Thread X
From Thread Y : 1
From Thread Y : 2
From Thread Y : 3
Exit Thread Y
From Thread Z : 1
From Thread Z : 2
From Thread Z : 3
Exit Thread Z

Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.