/* related posts with thumb nails */

Create two threads such that one of the thread print even no’s and another prints odd no’s up to a given range.:

class MyThread extends Thread

{

int initval, finalval;

MyThread(int iv,int fv)

{

initval=iv;

finalval=fv;

start();

}

public void run()

{

for(int i=initval;i<=finalval;i+=2)

{

System.out.println(i);

try

{

Thread.sleep(1000);

}

catch(Exception e){}

}

}

}

class ThreadTest

{

public static void main(String as[])

{

MyThread t1=new MyThread(1,6);

MyThread t2=new MyThread(2,6);

}

}

/* Output: */

1

2

3

4

5

6

Related Topics:

0 comments:

Post a Comment