Tuesday, 29 November 2016

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

No comments:

Post a Comment