O'Reilly logo

Just Java™ 2 by Peter van der Linden

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

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

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