Let the Thread Handle InterruptedExceptions

It’s considered good practice to let the executing thread handle InterruptedExceptions. Normally, you’d do this by declaring that method run throws the exception, rather than catching the exception. However, recall from Chapter 11 that when you override a method, the throws may contain only the same exception types or a subset of the exception types declared in the original method’s throws clause. Runnable method run does not have a throws clause in its original declaration, so we cannot provide one in line 21. To ensure that the executing thread receives the InterruptedException, line 32 first obtains a reference to the currently executing Thread by calling static method currentThread, then uses that ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.