1

I've used Thread.sleep for all kinds of different reasons, but one thing I've never understood is when an exception would occur during this try /catch block:

try {
    Thread.sleep(1000); // sleep for 1 second.
} catch (Exception x) {
    fail("Failed due to an exception during Thread.sleep!");
    x.printStackTrace();
}

What would have to occur within the computer to actually hit an exception on Thread.sleep? My best guess, would be that maybe the system clock has a once in a lifetime "skip of a beat" like a heartbeat, but how often does this happen..

So in essence, my question is: When executing Thread.sleep, what would have to occur internally for #sleep to throw an exception?

ddavison
  • 28,221
  • 15
  • 85
  • 110
  • The only checked exception i'm aware of is `InterruptedException`. (Note, if the code were catching something more specific than `Exception`, you probably wouldn't be scratching your head here. You should never `catch (Exception)`, for a number of reasons including that.) – cHao Jan 06 '14 at 20:16

5 Answers5

8

If you look in the JavaDoc for Thread.sleep() you see exactly what might happen:

Throws:
IllegalArgumentException - if the value of millis is negative
InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

An example on how to interrupt another thread might look like this:

public class Foo {
    public static void main(final String[] args) throws Exception {
        Thread sleepThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    System.out.println("Interrupted!");
                }
            }
        });
        sleepThread.start();
        Thread.sleep(500);
        sleepThread.interrupt();
    }
}

This will print

Interrupted!

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • so - what.. using something like `wait()` on the method? – ddavison Jan 06 '14 at 20:19
  • Excellent! Thanks for that. Will accept in 3 minutes ;) this code here gave me a practical (yet unpractical) example of how to use this, and when it would hit that exception. – ddavison Jan 06 '14 at 20:24
  • also, i hadn't had much experience with threading in general, so the doc didn't _particularly_ help – ddavison Jan 06 '14 at 20:28
  • @sircapsalot: No, it might not be the clearest Javadoc. Glad the example helped clear things up a bit. – Keppil Jan 06 '14 at 20:29
3

The Thread class defines an interrupt method. When it's called (obviously from another thread) by a thread and there's no security exception, an InterruptedException is thrown.

The purpose is, precisely (and normally), to interrupt the sleep.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

Thread.sleep(someValue) can fire InterruptedException.
More info about handling this exception and reson for this you can find here.

You can find a lot usefull answers on SO. Here is the one for example: When does Java's Thread.sleep throw InterruptedException?

Community
  • 1
  • 1
Bosko Mijin
  • 3,287
  • 3
  • 32
  • 45
0

When a thread interrupt is issued, for example if a signal was sent to shut the JVM down.

MarkL
  • 477
  • 4
  • 11
  • The usual reason for a thread being interrupted would be another thread calling the [`interrupt()`](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupt()) method on it. – ajb Jan 06 '14 at 20:20
  • Agreed, perhaps a poor example (although a real one). I actually up-voted Keppil's answer :D – MarkL Jan 06 '14 at 20:30
0

The Thread can throw an InterruptedException (which you should use instead of general Exception) when the Thread is interrupted (usually by the interrupt() method called from another Thread) instead of just waiting. It has nothing to do with the sleep() method.

ShadoWalkeR
  • 123
  • 1
  • 8