Creating a Thread

You create a thread by instantiating an object of type Thread. The Thread class encapsulates an object that is runnable. As mentioned, Java defines two ways in which you can create a runnable object:

Image You can implement the Runnable interface.

Image You can extend the Thread class.

Most of the examples in this chapter will use the approach that implements Runnable. However, Try This 11-1 shows how to implement a thread by extending Thread. Remember: Both approaches still use the Thread class to instantiate, access, and control the thread. ...

Get Java, A Beginner's Guide, 5th Edition, 5th 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.