Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (2.6k points)

I have tried to delay - or put to sleep - my Java program, but an error occurs.

I'm unable to use Thread.sleep(x) or wait(). The same error message appears:

unreported exception java.lang.InterruptedException; must be caught or declared to be thrown.

Is there any step required before using the Thread.sleep() or wait() methods?

1 Answer

0 votes
by (46k points)

You have a lot of studying leading you. From compiler errors throughout exception handling, threading and thread checks. But this will do what you require:

try {

    Thread.sleep(1000);                 //1000 milliseconds is one second.

} catch(InterruptedException ex) {

    Thread.currentThread().interrupt();

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...