Java create thread extends Thread

Java create thread extends Thread

Java create thread extends Thread

Summary: By the end of this tutorial "Java create thread extends Thread", you will be comfortable to create thread by extending Thread class.

After knowing what a thread is, it is the time for creating and using the thread. Java comes with two approaches for creation of thread.

1. By extending Thread class (of java.lang package)
2. By implementing Runnable interface (of java.lang package)

Program on Java create thread extends Thread

Aim: To create, start and execute thread by extending Thread class. The execution should print 0 to 9 numbers with an interval of 1000 milliseconds (1 second).


Java create thread extends Thread
Output screen of Java create thread extends Thread

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.

How to create threads by implementing Runnable interface?

Leave a Reply

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