Develop a java program to create two threads, one thread prints numbers from 1 to 10 and the other thread prints numbers from 10 to 1. First Thread should transfer control to second thread after printing second number

class ThreadOne extends Thread
{
 public void run()
 {
  try
  {
   for(int i=1;i<=10;i++)
   {
    System.out.println("Thread 1 : "+i);
    Thread.sleep(500);
   }
  }
  catch(InterruptedException e)
  {}
 }
}
class ThreadTwo extends Thread
{
 public void run()
 {
  try
  {
   for(int i=10;i>0;i--)
   {
    System.out.println("Thread 2 : "+i);
    Thread.sleep(1000);
   }
  }
  catch(InterruptedException e)
  {}
 }
}
class Exp9_2
{
 public static void main(String[] args) 
 {
  ThreadOne obj1 = new ThreadOne();
  ThreadTwo obj2 = new ThreadTwo();
  obj1.start();
  obj2.start();
 }
}
/*  OUTPUT!!!
Thread 1 : 1
Thread 2 : 10
Thread 1 : 2
Thread 1 : 3
Thread 2 : 9
Thread 1 : 4
Thread 1 : 5
Thread 2 : 8
Thread 1 : 6
Thread 1 : 7
Thread 2 : 7
Thread 1 : 8
Thread 1 : 9
Thread 2 : 6
Thread 1 : 10
Thread 2 : 5
Thread 2 : 4
Thread 2 : 3
Thread 2 : 2
Thread 2 : 1
*/

Copyright © LetML. Blogger Templates Designed by OddThemes