Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...