1

I'm wondering what might have been the reason Java Thread's doesn't accept a Runnable that allows throwing of InterruptedException. If instead an InterruptableRunnable was accepted, it would simplify a lot of code that will simply want to stop the thread when interrupted.

interface InterruptableRunnable {
    void run() throws InterruptedException;
}

Before:

new Thread(() -> {
    try {
        Thread.sleep(1000);
    }
    catch (InterruptedException e) {
        // Some confusion ensues here as to what to do;
        // - Should we restore the interrupt? For who? Guess not.
        // - Should we rethrow it? That would terminate thread exceptionally...
        // - Just return then? Sounds good...
        
        return;
    }
}).start();

Now, if instead you could use an InterruptableRunnable the code would be far simpler, and I think just as correct:

new Thread(() -> {
    Thread.sleep(1000);
}).start();

On an interrupt, the thread gets terminated exactly as you'd expect -- and the system could be smart enough not to consider an InterruptedException as an exceptional termination condition. If that's not what you want, you always have the option of catching the exception.

For similar reasons, what could have been the reasoning Thread#run() is not declared to throw InterruptedException...

john16384
  • 7,800
  • 2
  • 30
  • 44
  • 1
    I really struggle to understand "*Java Thread's doesn't accept a Runnable that allows throwing of InterruptedException*"... – Giorgi Tsiklauri Oct 02 '22 at 13:19
  • @GiorgiTsiklauri The standard `Runnable` that you can pass to `Thread`s constructor does not allow you to throw an `InterruptedException` from its `run` method because `InterruptedException` is a checked exception. – john16384 Oct 02 '22 at 13:20
  • So, it's not about `Runnable` but it's about *`run`* method.. it's now clearer. – Giorgi Tsiklauri Oct 02 '22 at 13:27

1 Answers1

1

Exceptions don't propagate across threads, for instance you need an UncaughtExceptionHandler to retrieve the exception thrown by another thread, see How to catch an Exception from a thread.

So putting InterruptedException in the throws clause creates a false expectation, the exception still can't be thrown in one thread and caught in another. That's the only reason I can think of for not doing this, they are trying to communicate to the developer that the toplevel run method for a thread needs to handle everything. Putting InterruptedException in the throws clause dilutes this message, as the developer isn't forced to deal with it.

But yes, they could have added InterruptedException to the throws clause of Runnable, that would make this case much simpler. But there are design goals in conflict. The designars want developers to be very aware of the exception behavior, including making InterruptedException checked, but it's not having it in the throws clause that forces people to deal with it. The goal of letting simple things be easy and having a thread call sleep without a catch is at odds with that so it wasn't implemented.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Thanks, but I'm not trying to catch an exception from a thread in another thread. The uncaught handler will log it, I know, I'm just asking why would `run` not have been declared to throw `InterruptedException` to make it easier to terminate threads with out having to go through the `try...catch` boilerplate. Any runtime exception terminates the thread, but an `InterruptedException` should for some reason be disallowed? – john16384 Oct 02 '22 at 13:32
  • @john16384: i think this is just another argument against checked exceptions, let the exception be thrown and kill the thread? – Nathan Hughes Oct 02 '22 at 13:37
  • certainly not, I'm highly in favor of checked exceptions, just missing why an exception intended to interrupt / stop a thread isn't part of the declaration of the `run` method which serves as the entry points for many threads; been looking through the JBS is something was proposed like this, and this is the most similar ticket I could find: https://bugs.openjdk.org/browse/JDK-8150342 – john16384 Oct 02 '22 at 13:41
  • @john16384, `InterruptedException` is not intended to stop a thread. It is intended to get a thread's attention. If _you_ want it to stop _your_ thread, then it's up to you to write a handler so that the thread can catch the exception and then _cleanly_ stop itself. – Solomon Slow Oct 02 '22 at 17:16