O'Reilly logo

PURE Java™ 2 by Kenneth Litwak

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required