Answer to Question #259585 in Java | JSP | JSF for Shadow

Question #259585

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).


1
Expert's answer
2021-11-01T05:32:39-0400


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();




    
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS