Latest :

Java Thread By Extending Thread Class – Java Tutorials

Java Thread by extending Thread class – Here we cover the complete tutorial and examples for java thread by extending thread class.

Generally, thread facilities are provided to a class in two ways:

  1. By extending Thread
  2. By implementing Runnable

In this page, we just concentrate more on extending Thread rather than implementing Runnable.

The class Thread helps to achieve multi threading. We can inherit Thread class to provide multi threading features to a class.

For Example:

When an object of the above class is created, that object contains Thread facilities. Once such an object is started the control creates a separate branch (Thread) and executes the code in that branch.

For Example:

The start method initiates a new branch, and starts the corresponding run() method. We should override the run() method in the corresponding class (Root here).

For Example:

For Example:

In the above program we have created an object of class Root and started it. When the thread is started the run() method of Root is executed in a separate branch (thread) and at the same time the remaining code in main() method is also executed.

The code in run() prints letters and at the same time the code in main() prints numbers. As both the codes execute simultaneously, we can see both (letters and numbers) printed together.

When we inherit the Thread class, the facilities (methods) of that class will be available to the sub class. The features include

Method name Functionality
sleep(long) suspends execution for the specified amount of time
sleep(long,int) suspends execution for the specified amount of time
activeCount(); returns the number of threads active currently
start(); creates new branch(thread) and invokes the corresponding run() method
run(); Actual code of the Thread is written here by us (programmer) in our thread class. The run() method in Thread class is just a dummy method and we override it in our class.
isAlive(); tells us whether the invoking object (Thread) is active or not. If the object is in running state, the method returns true. If the thread object is created but not yet started then the method returns false. If the thread execution is completed then also the method returns false.
setPriority(int); allows setting the priority of the thread. This method receives the priority (an integer) and sets it. the allowed range of values is from 1 to 10 where 1 indicates minimum priority and 10 indicates maximum priority. By default a thread holds a priority of 5 (normal priority)
getPriority(); gives us the current priority value of the thread
setName(String); allows us to set a name to the thread. By default a thread gets a name in sequence (like Thread-0, Thread-1, etc) after the main thread. If we want any specific name to a thread then we can specify it as a string.
getName(); returns a string that represents name of the thread
join(long) same as join() but allows the programmer to specify some time so that the main thread resumes its operation after the specified time (if the invoking thread has not completed execution)
join(long,int) same as join(long) but we can specify the amount of time to wait in nanoseconds also.
join() It makes the current thread wait till the invoking thread completes its execution. Suppose we are in main thread and created another thread and started it.

Then we have invoked the join() method with the another thread then the code in main will be suspended till the another thread completes its execution. otherwise ( if join() is not invoked) the code in main thread and another thread will be executed parellally.

setDaemon(boolean); Makes a thread daemon so that the daemon thread will be destroyed (stopped) automatically when its parent thread is completed/stopped. If a thread is not a daemon thread, then the thread will remain in running state even though its parent thread is complete.
isDaemon(); Tells whether a thread is daemon thread or not.

Out of the above mentioned methods, sleep(long), sleep(long,int), join(), join(long) and join(long,int) would throw an InterruptedException.

So we should take care of handling the situation when these methods are used.

Similarly start(), setName(String), join(long) and join(long,int) are synchronized methods. So cannot use them parallel on a single Thread object.

The sleep(long), sleep(long,int) and activeCount() are static methods so they can be used without creating an object of Thread.

isAlive(), setPriority(int), getPriority(), setName(String), getName(), join(long), join(long,int), join(), setDaemon(boolean) and isDaemon() are final methods. So we cannot overwrite them.

All of them (mentioned above) are public methods so we can accessed from anywhere.

If you have any doubts related to Java Thread by extending Thread class and examples which we shared above just do leave a comment here.

x

Check Also

java for loop

Java For Loop – Tutorial With Examples | Loops

Java for loop tutorial with examples and complete guide for beginners. The below article on ...