How to Make Your Classes Threadable

There are two approaches to making your classes behave as threads:

  • Extending the Thread class

  • Implementing the Runnable interface

Which approach you choose depends on your application's needs and the constraints placed on it. Giving your class the capability to run as a thread does not automatically make it run as such. When a class is called runnable, that means it has the ability to act as a separate thread, but not necessarily that it is running as a separate thread. The reason for this is that the start method that is defined in the Runnable interface must be called to start the thread running. You'll learn more about the start method later in this chapter.

Get Special Edition Using Java 2 Standard 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.