Reputation: 607
try
{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
I understand what the first part is doing. But what is the catch part waiting for? Like what needs to happen to execute this part.
Upvotes: 1
Views: 149
Reputation: 1480
This is relevant to multithreaded applications.When you have reference to an object of a class that either implements Runnable
or extends Thread
, and inside that object, a Thread.sleep(i)
method has been idling the process, you can call reference.interupt()
on that reference and it will trigger the InterruptedException
. Here's an example:
In the below code, the Scratch
class's run
method goes to sleep for 60000 ms (or 1 minute) and prints "I've been interrupted" if it receives an InterruptedExection
. The way the code is written, it will not receive this exception and will sleep the entire time. But run it a second time with the line //scratch.interrupt();
uncommented and see what happens.
package scratch;
/**
*
* @author bstidham
*/
public class Scratch extends Thread {
public void run() {
System.out.println("Hello from a thread!");
System.out.println("Going to sleep for 1 minute...");
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
System.out.println("I've been interrupted!");
}
System.out.println("Awake now");
}
public static void main(String args[]) {
Scratch scratch = new Scratch();
scratch.start();
//scratch.interrupt();
}
}
Upvotes: 0
Reputation: 4534
public class InterruptedException
extends Exception
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.
Best Explanation is Given ╠══ HERE
Upvotes: 1
Reputation: 6149
Every piece of code is run by a "thread". You can think of that as a little virtual processor dedicated to running that particular piece of code.
By calling the Thread.sleep()
method, you put this thread in the WAITING
state for a certain amount of time. While WAITING
, the thread cannot do anything, and in particular, it cannot "hear" calls from other threads.
The only way to wake up the thread and having it run its code again, is to sent it an interruption, which is a very strong signal. This is done by calling the interrupt()
method of this thread, from another thread.
When waked up, the thread goes to the RUNNING
state again, but to signal the developer that it was waked up earlier than expected, it throws an InterruptedException
, which is a checked exception. This is why you have to catch it and deal with it.
Upvotes: 1
Reputation: 4637
You thread is going to sleep for 5 seconds.
If another thread tries to wake this thread up (Interrupt). It will end up in the exception block and your stacktrace will be printed.
Upvotes: 1
Reputation: 62884
Because the Thread#sleep() method is defined to throw an InterruptedException
under some circumstances, the developer should take care of it when this exact exception occurs. This is why the catch
block is needed - to hold the logic which handles the InterruptedException
Upvotes: 1
Reputation: 551
The sleep method halts execution of the current thread, in your case 5000 milliseconds (5 seconds). This method however throws InterruptedException which has to be caught.
Upvotes: 1
Reputation: 4923
If the Thread is interrupted then it will throw the exception and it will be catched in the catch
block.
Upvotes: 0
Reputation: 35597
If InterruptedException
throws(Thread interruption may cause this) from try
block that will catch by catch
block and as you define in the catch block,it will print the Exception
stack trace.
Upvotes: 0