Write a program to illustrate creation of threads using extending a Thread class. (start method start
each of the newly created thread. Inside the run method there is sleep() for suspend the thread for
500 milliseconds. Write the method to display your Index No 5 times using a loop as follows).
package indexno;
class MyThread extends Thread{
public static int count = 0;
@Override
public void run(){
while(MyThread.count <= 5){
try{
System.out.println("Index number here: "+(++MyThread.count));
Thread.sleep(500);
} catch (InterruptedException iex) {
System.out.println("Exception in thread: "+iex.getMessage());
}
}
}
}
public class IndexNo extends Thread {
public static void main(String[] args) throws InterruptedException {
System.out.println("Starting Main Thread...");
MyThread m = new MyThread();
m.start();
}
}
Comments
Leave a comment