0

Possible Duplicate:
When does Java's Thread.sleep throw InterruptedException?

I saw we have to catch InterruptedException in the Thread.Sleep method, why? I never saw this exception occured in the real time. Any clue?

Community
  • 1
  • 1
user496949
  • 83,087
  • 147
  • 309
  • 426

2 Answers2

3

This happens if thread#1 is sleeping, and another thread interrupts it. This is usually an attempt to stop thread#1. For example, thread#1 is sleeping while doing some long-running task (perhaps in background) and the user hits a cancel button.

user949300
  • 15,364
  • 7
  • 35
  • 66
  • 1
    sleep can only happen when we purposely sleep the thread. it cannot process some long-running task. – user496949 Jan 08 '12 at 03:35
  • 1
    True. But many long running background tasks sleep for a while as part of a loop to periodically poll a database, check a file folder etc. – user949300 Jan 08 '12 at 04:08
1

The IterruptedExeption is thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception. The following code can be used to achieve this effect:

if (Thread.interrupted())  // Clears interrupted status!
{
  throw new InterruptedException();
}

Every thread has a Boolean property associated with it that represents its interrupted status. The interrupted status is initially false; when a thread is interrupted by some other thread through a call to Thread.interrupt(), one of two things happens. If that thread is executing a low-level interruptible blocking method like Thread.sleep(), Thread.join(), or Object.wait(), it unblocks and throws InterruptedException. Otherwise, interrupt() merely sets the thread's interruption status. Code running in the interrupted thread can later poll the interrupted status to see if it has been requested to stop what it is doing; the interrupted status can be read with Thread.isInterrupted() and can be read and cleared in a single operation with the poorly named Thread.interrupted(). See.


There is no way to simply stop a running thread in java (don't even consider using the deprecated method stop()). Stopping threads is cooperative in java. Calling Thread.interrupt() is a way to tell the thread to stop what it is doing. If the thread is in a blocking call, the blocking call will throw an InterruptedException, otherwise the interrupted flag of the thread will be set.

The problem is that blocking calls like sleep() and wait(), can take very long till the check can be done. Therefore they throw an InterruptedException. (However the isInterrupted is cleared when the InterruptedException is thrown.)

Lion
  • 18,729
  • 22
  • 80
  • 110
  • @line, can this exception be throwed when a thread sleep? at least, I never saw. – user496949 Jan 08 '12 at 03:31
  • It was mentioned in the answer. *"Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread. "* – Lion Jan 08 '12 at 03:36
  • Thrown when a waiting thread is activated before the condition it was waiting for has been satisfied. – Lion Jan 08 '12 at 03:43