Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

Pgm 9 || Illustrate creation of thread by

 a) Extending Thread class.


class A extends Thread

{

public void run()

{

for (int i=1;i<=5;i++)

{

System.out.println("Thread : i="+i);

}

System.out.println("Exit from Thread");

}

}

class Pgm9a

{

public static void main(String args[])

{

new A().start();

}

}



-----------------------------------------------------------------------------------------------------------------------


b) Implementing Runnable Interfaces

class X implements Runnable

{

public void run()

{

for(int i=1;i<=5;i++)

{

System.out.println("Thread X"+i);

}

System.out.println("end of ThreadX");

}

}

class Pgm9b

{

public static void main(String args[])

{

X runnable = new X();

Thread threadX=new Thread (runnable);

threadX.start();

}

}

Post a Comment

0 Comments