Basic Thread Method Example in Java


Here is the simple program for printing basic example of thread methods in Java.

Source Code

import java.io.*;
class first extends Thread
{
  public void run()
  {
   for(int i=0;i<6;i++)
   {
    if(i==3)
    {
      yield();
    }
    System.out.println("First :"+i);
   }
  }
}
class second extends Thread
{
  public void run()
  {
  	for(int i=0;i<6;i++)
    {
      if(i==5)
      {
       try
       {
        sleep(1000);
       }
       catch(Exception e)
       {
       }
      }
      System.out.println("Second :"+i);
    }
  }
}
class third extends Thread
{
  public void run()
  {
    for(int i=0;i<6;i++)
    { 
      if(i==2)
      {
        stop();
      }
      System.out.println("Third"+i);
    }
  }
}
class leader
{
  public static void main(String args[]) throws IOException
  {
   first a  = new first();
   second b = new second();
   third c  = new third();
   a.start();
   b.start();
   c.start();
  }
}
To download raw file Click Here

Output

Third0
First :0
Second :0
First :1
Third1
First :2
Second :1
First :3
Second :2
First :4
Second :3
First :5
Second :4
Second :5


Learn All in Tamil © Designed & Developed By Tutor Joes