Chapter 10. Threading

From its earliest days, Java has been a multithreaded environment. While the threading capabilities are formidable in Java 1.4, Tiger introduces a whole new slew of concurrency utilities, allowing for further tweaking of your multithreaded programs.

Handling Uncaught Exceptions in Threads

Normally a Java thread (represented by any class that extends java.lang.Thread) stops when its run( ) method completes. In an abnormal case, such as when something goes wrong, the thread can terminate by throwing an exception. This exception trickles up the thread’s ThreadGroup hierarchy, and if it gets to the root ThreadGroup, the default behavior is to print out the thread’s name, exception name, exception message, and exception stack trace.

To get around this behavior (at least in Java 1.4 and earlier), you’ve got to insert your own code into the ThreadGroup hierarchy, handle the exception, and prevent delegation back to the root ThreadGroup. While this is certainly possible, you’ll have to define your own subclass of ThreadGroup, make sure any Threads you create are assigned to that group, ...

Get Java 5.0 Tiger: A Developer's Notebook 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.