Two Ways to Obtain a New Thread

image

There are two ways to obtain a new thread of control in Java. Either extend the Thread class, or write a class to implement the java.lang.Runnable interface and use it in the Thread constructor. The first way can be used only if your class doesn't already extend some other class (because Java disallows multiple parents).

  1. Extend class java.lang.Thread and override run():

    class Plum extends Thread {     public void run() { /* more code */ }}Plum P = new Plum();P.start();

    or

  2. Implement the Runnable interface (the class Thread itself is an implementation of Runnable), and use that class in the Thread constructor: ...

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