Creating Threads

There are two ways to create a Thread object. Listing 11.1 shows the approach you should use most often (from an object-oriented design perspective).

Creating Threads with Runnable Objects: Code Example

You can create a Thread in two ways. You may create a Thread using an instance of a class that implements the Runnable interface. This is the best choice generally from an object-oriented design perspective. Or, you can subclass the Thread class to make an object that inherits from Thread. The former, generally superior approach is shown in Listing 11.1.

Code Listing 11.1. Creating a Thread with a Runnable Object (ThreadDriver1.java)
 class BookFinder implements Runnable // Threads need a Runnable object for their constructor. ...

Get PURE Java™ 2 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.