Basic Thread Example in Java


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

Source Code

import java.io.*;
class first extends Thread
{
  public void run()
  {
    for(int i=0;i<20;i+=2)
    System.out.println("Even Number:"+i);
  }
}
class second extends Thread
{
  public void run()
  {
    for(int i=1;i<20;i+=2)
    System.out.println("Odd Number:"+i);
  }
}
class multi
{
  public static void main(String args[])throws IOException
  {
   System.out.println("Start Thread Program");
   first f=new first();
   second s=new second();
   f.start();               
   s.start();
   System.out.println("End Thread Program");
  }
}
To download raw file Click Here

Output

Start Thread Program
End Thread Program
Even Number:0
Odd Number:1
Even Number:2
Even Number:4
Odd Number:3
Even Number:6
Odd Number:5
Even Number:8
Odd Number:7
Even Number:10
Odd Number:9
Even Number:12
Even Number:14
Odd Number:11
Even Number:16
Odd Number:13
Even Number:18
Odd Number:15
Odd Number:17
Odd Number:19


Learn All in Tamil © Designed & Developed By Tutor Joes