Java Create Threads

Java Create Threads

Java Create Threads

2. Creating a thread by implementing Runnable interface

Now, we write another program, with the same functionality (printing 0 to 9 with an interval of 1000 milliseconds), but implementing Runnable interface instead of the earlier Thread class.

Java Create Threads

In the above program, the object, d1, is not implicitly a thread because it is not extending Thread class; but implements Runnable interface. It takes one more statement to get the run() method executed.

Demo d1 = new Demo();
Thread t1 = new Thread(d1);

Create an object, t1, of Thread class and pass the d1 object as parameter. Then, start the thread t1 with start() method. It indirectly calls the run() method of Demo class. This is extra work when Runnable interface is implemented instead of extending Thread class.

Java Create Threads: Extends vs Implements

When to use Thread or when to prefer Runnable. Even though no difference exists in the output, there is a reason and necessity to implement Runnable interface in some cases. If your class already extends another class like Frame or Applet, you cannot extend Thread class as Java does not support multiple inheritance. To overcome this problem, designers introduced another style of implementing Runnable interface. If your class does not extend another class, you can go for Thread class.

Java Create Threads: Why implementing Runnable approach is better than Thread extending?

In general, not exactly pertaining threads, when a method is called by an object, the JRE checks for overloaded methods exist or not, super class method is overridden or not (especially when a subclass object is assigned to super class object or reference variable) etc. It may be the same case with run() method of Thread class. If you implement Runnnable, JRE checks run() is implemented or not but does not check all the other round about rules. For this reason, everyone says implementing Runnable is speedier than Thread extends.

19 thoughts on “Java Create Threads

  1. Sunita Bansal .


    Why your write class thread extends object implements Runnable. Even we say once a class does not extends any class it has default super class that is object. Or it has different meaning. I am not getting

    1. S. Nageswara Rao, Corporate Trainer

      Runnable interface contains method run(). If you pass an object of a class that implements Runnable interface, the thread object implicitly calls the run() method of the class. This is adjustment, the designers planned to call the callback method run(). To achieve this exactly, if you pass an object that does not implements Runnable interface, it is an error.

      The same thing you can observe with throw keyword. You must throw any object that must be a Throwable or subclass of Throwable.

    1. S. Nageswara Rao, Corporate Trainer

      What you say is one of the good examples to prove that Java is a robust language. If you call start() method, it calls implicitly run() method. If you call run() directly, certain activities that JVM should perform on the thread would not be done. For example, when thread finishes its job, it will be garbage collected or when you call join() method, the parent thread should wait until the child dies. So, call start() instead of run().

      Many examples exist in Java like this.

    2. Abhishek Rajan

      Respected Rao Sir,

      Please read my answer and guide me if I am worng:

      When we call start() method.. the call of the run() method is done in the code of start() method. When we call run() method instead of start() in order to run the thread it will be called but it will not be done in a separate thread. In order to call in the separate stack we should call start() method..

      Thank You

Leave a Reply

Your email address will not be published. Required fields are marked *