Why do I need to handle an exception for Thread.sleep()? Why do I need to handle an exception for Thread.sleep()? multithreading multithreading

Why do I need to handle an exception for Thread.sleep()?


If a method is declared in a way that it can throw checked exceptions (Exceptions that are not subclasses of RuntimeException), the code that calls it must call it in a try-catch block or the caller method must declare to throw it.

Thread.sleep() is declared like this:

public static void sleep(long millis) throws InterruptedException;

It may throw InterruptedException which directly extends java.lang.Exception so you have to catch it or declare to throw it.

And why is Thread.sleep() declared this way? Because if a Thread is sleeping, the thread may be interrupted e.g. with Thread.interrupt() by another thread in which case the sleeping thread (the sleep() method) will throw an instance of this InterruptedException.

Example:

Thread t = new Thread() {    @Override    public void run() {        try {            System.out.println("Sleeping...");            Thread.sleep(10000);            System.out.println("Done sleeping, no interrupt.");        } catch (InterruptedException e) {            System.out.println("I was interrupted!");            e.printStackTrace();        }    }};t.start();     // Start another thread: tt.interrupt(); // Main thread interrupts t, so the Thread.sleep() call               // inside t's run() method will throw an InterruptedException!

Output:

Sleeping...I was interrupted!java.lang.InterruptedException: sleep interrupted    at java.lang.Thread.sleep(Native Method)    at Main$1.run(Main.java:13)


One Thread can communicate with and interact with another Thread, and one way that it can do it is by interrupting it: if t is another Thread, you can call t.interrupt() to ask it politely to stop what it's currently doing. This is in particular something you might want to do if t is sleeping: you might want to wake it up. What it does is to cause an InterruptedException in t's Thread.sleep() method, so that it can catch it and respond. Because of this, any time you use Thread.sleep() to make the current thread go to sleep, you have to deal with the possibility of an InterruptedException in case another thread decides to wake it up.

In your case, you've only got one Thread, so you know that there can't be an InterruptedException from elsewhere in your code. But it's a not uncommon thing to want to do in multi-threaded code.


 class Demo extends Thread{    public void run() {        for (int i = 0; i <10; i++) {        system.out.println("hello Ziyad");        thread.sleep(1000);    }} }            public class Threddemo{        public static void main(string[] args) throws interruptedexception {            Demo t=new Demo();            Demo t2=new Demo();        t.start();        t2.start();            }}

Suppose We have two Thread t and t2 and t is executing while executing, t2 came and t2 is also start executing but t is not finish yet there the thread get interrupted and you lose your data.In above example t thread is running and when in spleeping mode, and there t2 came and start executing suddenly t get up but t2 is running this is chance of interruptedexception and data lose to avoid this we use interruptedexception