Saturday, 26 April 2014

Ex 24: Write a program for generating 2 threads, one for printing even numbers and the other for printing odd numbers.

class even extends Thread
{
Thread t=null;
even()
{
t=new Thread(this);
start();
}
public void run()
{
try
{
for(int i=2;i<50;i+=2)
System.out.print(i+" ");
Thread.sleep(100);
}
catch(Exception e)
{System.out.println("thread interepted");}
}
}
class odd extends Thread
{
Thread t=null;
odd()
{
t=new Thread(this);
start();
}
public void run()
{
try
{
for(int i=1;i<50;i+=2)
System.out.print(i+" ");
Thread.sleep(100);
}
catch(Exception e)
{System.out.println("thread interepted");}
}
}
class s07_03
{
public static void main(String arg[])
{
even e=new even();
odd o=new odd();
}
}

No comments:

Post a Comment