Develop a java program to create two threads such that one thread will print odd numbers and the other thread will print even numbers between 1 to 20 numbers.

class EvenThread implements Runnable
{
 Thread t;
 EvenThread()
 {
  t = new Thread(this, "Even Thread");
  t.start();
 }
 public void run()
 {
  try
  {
   for(int i=1;i<=20;i++)
   {
    if(i%2==0)
    {
     System.out.println("Even Thread: "+i);
     Thread.sleep(500);
    }
   }
  }
  catch(InterruptedException e)
  {}
 }
}
class OddThread implements Runnable
{
 Thread t;
 OddThread()
 {
  t = new Thread(this, "Odd Thread");
  t.start();
 }
 public void run()
 {
  try
  {
   for(int i=1;i<=20;i++)
   {
    if(i%2!=0)
    {
     System.out.println("Odd Thread: "+i);
     Thread.sleep(500);
    }
   }
  }
  catch(InterruptedException e)
  {}
 }
}
class Exp9_1
{
 public static void main(String[] args) 
 {
  new EvenThread();
  new OddThread();
 }
}
/*  OUTPUT!!!
Even Thread: 2
Odd Thread: 1
Even Thread: 4
Odd Thread: 3
Even Thread: 6
Odd Thread: 5
Even Thread: 8
Odd Thread: 7
Even Thread: 10
Odd Thread: 9
Even Thread: 12
Odd Thread: 11
Even Thread: 14
Odd Thread: 13
Even Thread: 16
Odd Thread: 15
Even Thread: 18
Odd Thread: 17
Even Thread: 20
Odd Thread: 19
*/
Copyright © LetML. Blogger Templates Designed by OddThemes