//Java program to Print Odd and Even Number
Using Thread .
public class thread1 extends Thread
{
boolean isEven;
int count;
int r;
public
thread1(boolean b,int r)
{
isEven=b;
count=r;
}
public void run()
{
if(isEven)
even();
else
odd();
}
public void even()
{
for(r=1;r<=count;r++)
if(r%2==0)
{
System.out.println(" \t Even :"+r);
try
{
sleep(2000);
}
catch(Exception
e)
{
System.out.println(e);
}
}
}
public void odd()
{
for(r=1;r<=count;r++)
if(r%2!=0)
{
System.out.println("\t Odd
:"+r);
try
{
sleep(2000);
}
catch(Exception
e)
{
System.out.println(e);
}
}
}
public static void main(String[] args)
{
thread1 t1=new thread1(false,20);
thread1 t2=new thread1(true,20);
t1.start();
t2.start();
}
}
No comments:
Post a Comment