Java Create Threads

Java Create Threads


Java Create Threads

Threads can be created in two ways in Java.

  1. By extending Thread class.
  2. By implementing Runnable interface.

Why Java requires two types and when to use which type, we will see later.

Signature of Thread class


public class Thread extends Object implements Runnable

Observe, the Thread class implements Runnable interface.

1. Creating a thread by extending Thread class

To create a thread, the first style and used generally is by extending Thread class.

Read the following program and see how simple to create a thread in Java. The program prints 0 to 9 numbers with a gap of 1000 milliseconds (1 second); it is the simplest way to create and control the thread.

Java Create Threads

In the above program, Demo class extends Thread. To create a thread in Java, just extend Thread class and create an object, the object becomes a thread implicitly. Now, d1 is a thread of Demo class. Actually, d1 is an object of Demo class but we are calling it as a thread (because Demo extends Thread). This is how Java makes thread manipulation easy to the programmer; some more concepts that are made easy, we get later.

Demo d1 = new Demo();
d1.start();

The thread, d1, is created, occupies some memory, but still it is inactive. An inactive thread is not eligible for microprocessor time even if the processor is idle. To make the thread active, call start() method on the thread. Now, d1 becomes active and eligible for a time-slice. start() is a method of Thread class. Forgetting to call the start() method makes the thread to die in inactive state itself without performing the job assigned.

public void run()

Whenever the thread is started, the first and foremost job it does is calling the run() method. It is a predefined thread of Thread class. This method is the heart of the thread and whatever code is expected to run by the thread should be put here. When the execution of run() method is over, the thread dies implicitly; naturally, its job is over.

Thread.sleep(1000);

sleep() is a method of Thread class inherited from Runnbale interface and overridden.

sleep() method signature

public static native void sleep(long milliseconds) throws InterruptedException

It is a static method throwing InterruptedException. Being a checked exception, whenever the sleep() method is used, the InterruptedException must be handled, else the code does not compile. The parameter 1000 is of long data type which indicates the inactive period of time in milliseconds. By calling sleep() method, the thread is made inactive for 1000 milliseconds (that is, 1 second). When the sleep time is over, the thread becomes implicitly active and eligible for microprocessor time (timeout of thread). For every iteration, the thread, d1, becomes inactive for 1000 milliseconds. sleep() is the easiest way to make the thread inactive and other ways we see later.

19 thoughts on “Java Create Threads

  1. Sunita Bansal .

    Sir,

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

Leave a Reply

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