Print odd and even numbers using 2 threads...

public class CountDownApp
{
public static void main(String[] args)
{
Thread count1 = new CountDownEven();
Thread count2 = new CountDownOdd();
count1.start();
//try{
//count2.join();
count2.start();

//}
//catch(Exception e){}



}
}

class CountDownEven extends Thread
{
public void run()
{
for(int i=10;i>0;i-=2)
{
System.out.println(this.getName()+"count EVEN"+i);
Thread.yield();

}
}

}

class CountDownOdd extends Thread
{
public void run()
{
for(int i=9;i>0;i-=2)
{
System.out.println(this.getName()+"count ODD"+i);
Thread.yield();

}
}

}
Output:



C:\Documents and Settings\Maddy\Desktop>javac CountDownApp.java

C:\Documents and Settings\Maddy\Desktop>java CountDownApp
Thread-0count EVEN10
Thread-1count ODD9
Thread-0count EVEN8
Thread-1count ODD7
Thread-0count EVEN6
Thread-1count ODD5
Thread-0count EVEN4
Thread-1count ODD3
Thread-0count EVEN2
Thread-1count ODD1
Runnable interface:

Comments

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams

SCJP1.6 Question and Answers