4.18. Java threads and exceptions

In Java, threads are created from a class which either inherits from the class Thread or implements the interface Runnable. In either case we specify the activity to be carried out by providing a run method. In the case of a thread inheriting from the Thread class, the run method overrides the run method of Thread, which otherwise has an empty method body. Producing threads via the mechanism of implementing the interface Runnable allows us to produce a class which can be used to create threads whilst still being able to inherit from some class other than Thread. Java only has single inheritance.

The program in Figure 4.32 explicitly creates two threads, using a thread that specializes Thread. The program in ...

Get Operating Systems: Concurrent and Distributed Software Design 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.