Appendix B. Exceptions and Errors

So far we have discussed the Thread class and its related classes with little attention to error conditions. One of the reasons for this is the lack of actual error conditions, because the threading system does not depend on external hardware. Classes that deal with the disk or network have to handle all possible error conditions that exist due to the failure of the hardware. Databases or the windowing system need an error system, which allows the programmer better control over the interaction between application, data structures, and user.

But what is necessary to deal with threading? Threading is a processor resource. Starting another thread means simply setting up data structures that allow the processor to run code and that configure the processor to switch between the different threads. As we discussed in Chapter 6, threading may involve the operating system; it may involve more than one processor. But in any case, the only hardware involved is the processor(s) and possibly additional memory. The synchronization system also only involves memory: there is not much that can go wrong when there is little hardware involved. We can get processor or memory errors, but these errors generally affect the entire virtual machine and not an individual thread.

The only errors that we need to be concerned with, then, are programmer errors. It is possible for the programmer accidentally to configure the threads incorrectly or to use threads or the synchronization ...

Get Java Threads, Second 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.