Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must I wrap every Thread.sleep() call in a try/catch statement? [duplicate]

I am trying to write my first multi-threaded program in Java. I can't understand why we require this exception handling around the for loops. When I compile without the try/catch clauses it gives an InterruptedException.

Here is the message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type InterruptedException

But when run with the try/catch, the sysout in the catch blocks are never displayed - implying no such exception was caught anyway!

public class SecondThread implements Runnable {
    Thread t;
    
    SecondThread() {
        t = new Thread(this, "Thread 2");
        t.start();
    }

    public void run() {
        try {
            for (int i=5; i>0; i--) {
                System.out.println("thread 2: " + i);
                Thread.sleep(1000);
            }
        }
        catch (InterruptedException e) {
            System.out.println("thread 2 interrupted");
        }
    }
}

public class MainThread {

    public static void main(String[] args) {
        new SecondThread();
    
        try {
            for (int i=5; i>0; i--) {
                System.out.println("main thread: " + i);
                Thread.sleep(2000);
            }
        }
        catch (InterruptedException e) {
            System.out.println("main thread interrupted");
        }
    }
}
like image 689
Sergio Gliesh Avatar asked Mar 13 '23 02:03

Sergio Gliesh


1 Answers

The method Thread.sleep throws InterruptedException if it detects that the current thread has its interrupt flag set, waking up from its sleep early and allowing you to use the exception to relocate control to somewhere outside the current flow. That flag gets set only if something calls interrupt on the thread.

Since your program doesn't call interrupt on any of the threads, InterruptedException will not get thrown when you run this program. The compiler still requires you to catch the exception since it is a checked exception declared on the sleep method.

If you add a method like this to SecondThread

public void cancel() {
    t.interrupt();
}

then call cancel in the main method, like this:

public static void main(String[] args){
    SecondThread secondThread =  new SecondThread();

    try{
        for(int i=5 ; i>0 ; i--){
            System.out.println("main thread: " + i);
            Thread.sleep(2000);
            secondThread.cancel();
        }
    }
    catch(InterruptedException e){
        System.out.println("main thread interrupted");
    }
}

you'll see the println where the InterruptedException gets caught in SecondThread's run method.

Compile errors show up in eclipse under the Problems tab, in addition to getting called out in the editor by getting underlined in red, they show up as you're editing the code. When you run this program any exceptions will get written to the console, along with the program output.

like image 138
Nathan Hughes Avatar answered Apr 06 '23 00:04

Nathan Hughes